aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/me/texx/Texx/PhotoEditorActivity.kt
blob: 7aa9b07dd1abdef565b86c90b21bb83279be3577 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package me.texx.Texx

import android.content.Context
import android.graphics.Color.RED
import android.graphics.Typeface
import android.net.Uri
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
import ja.burhanrashid52.photoeditor.OnPhotoEditorListener
import ja.burhanrashid52.photoeditor.PhotoEditor
import ja.burhanrashid52.photoeditor.PhotoEditorView
import ja.burhanrashid52.photoeditor.ViewType
import kotlinx.android.synthetic.main.activity_photo_editor.*
import me.texx.Texx.util.ThemeUtil.getThemeName


/**
 * Activity which will be shown after you've taken a picture
 * Previews the taken picture and posts it if you want
 */
class PhotoEditorActivity : AppCompatActivity() {
    /**
     * Set initial configuration
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        matchDressCode()
        super.onCreate(savedInstanceState)
        dressCodeName = getThemeName(this)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.activity_photo_editor)

        initPhotoEditor()
    }

    private lateinit var photoEditor: PhotoEditor
    private var currentlyDrawing = false
    private var currentlyTyping = false
    private var typingColor = RED

    private fun initPhotoEditor() {
        val filepath = intent.getStringExtra("filepath")
        val imageEditorView = findViewById<PhotoEditorView>(R.id.image_editor)
        imageEditorView.source.setImageURI(Uri.parse(filepath))

        photoEditor = PhotoEditor.Builder(this, imageEditorView)
                .setPinchTextScalable(true)
                .setDefaultTextTypeface(Typeface.DEFAULT)
                .build()

        setButtonListeners()
    }

    private fun setButtonListeners() {
        // undo button
        button_undo.setOnClickListener { photoEditor.undo() }

        // redo button
        button_redo.setOnClickListener { photoEditor.redo() }

        // draw button
        button_draw.setOnClickListener { toggleDrawing() }

        // emoji button
        button_emoji.setOnClickListener { toggleEmojiMenu() }

        // type button
        button_type.setOnClickListener { toggleTextEditing("") }

        // text editing "view" for on photo typing
        text_edit.setOnEditorActionListener { _, actionId, _ -> return@setOnEditorActionListener textEditorViewHandler(actionId) }

        // color seekbar
        seekbar_color.setOnColorChangeListener { _, _, color -> colorHandler(color) }

        // text editing box on photo (long click)
        photoEditor.setOnPhotoEditorListener(object : OnPhotoEditorListener {
            override fun onEditTextChangeListener(rootView: View, text: String, colorCode: Int) {
                toggleTextEditing(text)
            }

            override fun onAddViewListener(viewType: ViewType, numberOfAddedViews: Int) {}
            override fun onRemoveViewListener(numberOfAddedViews: Int) {}
            override fun onRemoveViewListener(viewType: ViewType, numberOfAddedViews: Int) {}
            override fun onStartViewChangeListener(viewType: ViewType) {}
            override fun onStopViewChangeListener(viewType: ViewType) {}
        })
    }

    private fun toggleEmojiMenu() {

    }

    fun onEmojiSelection() {

    }

    private fun toggleDrawing() {
        currentlyDrawing = !currentlyDrawing
        photoEditor.setBrushDrawingMode(currentlyDrawing)

        if (currentlyDrawing) seekbar_color.visibility = View.VISIBLE
        else {
            seekbar_color.visibility = View.GONE
            button_draw.background = ContextCompat.getDrawable(this, R.drawable.btn_edit)
        }
    }

    private fun toggleTextEditing(text: String) {
        currentlyTyping = !currentlyTyping
        if (currentlyTyping) {
            seekbar_color.visibility = View.VISIBLE
            text_edit.visibility = View.VISIBLE
            text_edit.imeOptions = EditorInfo.IME_ACTION_DONE
            text_edit.setText(text)
            text_edit.requestFocusFromTouch() // set focus
            val inputManager = this@PhotoEditorActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager.showSoftInput(text_edit, 0)
        } else {
            button_type.background = ContextCompat.getDrawable(this, R.drawable.btn_text)
            seekbar_color.visibility = View.GONE
            text_edit.visibility = View.GONE
        }
    }

    private fun textEditorViewHandler(actionId: Int): Boolean {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            photoEditor.addText(text_edit.text.toString(), typingColor)
            text_edit.visibility = View.GONE
        }
        return true
    }

    private fun colorHandler(color: Int) {
        if (currentlyDrawing) {
            photoEditor.brushColor = color
            button_draw.setBackgroundColor(color)
        } else if (currentlyTyping) {
            typingColor = color
            button_type.setBackgroundColor(color)
        }
    }
}