root / ofxArgos / src / UI Panels / EditorCore.cpp @ 69
View | Annotate | Download (6.1 KB)
| 1 | /***********************************************************************
|
|---|---|
| 2 | |
| 3 | Copyright (c) 2009, 2010 Dimitri Diakopoulos, http://argos.dimitridiakopoulos.com/ |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without modification, |
| 6 | are permitted provided that the following conditions are met: |
| 7 | |
| 8 | 1. Redistributions of source code must retain the above copyright notice, |
| 9 | this list of conditions and the following disclaimer. |
| 10 | |
| 11 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | this list of conditions and the following disclaimer in the documentation and/or |
| 13 | other materials provided with the distribution. |
| 14 | |
| 15 | 3. The name of the author may not be used to endorse or promote products derived |
| 16 | from this software without specific prior written permission. |
| 17 | |
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | ARE DISCLAIMED. IN NOEVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 22 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 25 | AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 27 | OF THE USE OF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | *************************************************************************/ |
| 30 | |
| 31 | #include "EditorCore.h" |
| 32 | |
| 33 | const float gridsize = 20.00; |
| 34 | |
| 35 | EditorPanel::EditorPanel() {}
|
| 36 | |
| 37 | void EditorPanel::init(ofxArgosUI &gui){
|
| 38 | |
| 39 | // Give the editor access to the GUI
|
| 40 | this->gui = &gui; |
| 41 | |
| 42 | // Give actions access to the edtior
|
| 43 | rControl = new resizeControl(this); |
| 44 | mControl = new moveControl(this); |
| 45 | |
| 46 | // Add a panel to the GUI for the editor
|
| 47 | editor = gui.addPanel("Widget Editor", 10, 20, 210, 180); |
| 48 | |
| 49 | // Populate the editor panel with controls for editing
|
| 50 | populate(); |
| 51 | |
| 52 | // Prevent the editor from being able to focus itself
|
| 53 | editor->lockPanel(); |
| 54 | |
| 55 | // But, Reset text inputs so they _can_ focus;
|
| 56 | label->canfocus = true;
|
| 57 | x->canfocus = true;
|
| 58 | y->canfocus = true;
|
| 59 | w->canfocus = true;
|
| 60 | h->canfocus = true;
|
| 61 | osc->canfocus = true;
|
| 62 | |
| 63 | } |
| 64 | |
| 65 | void EditorPanel::update(){
|
| 66 | if (stateManager::editing) {
|
| 67 | if (focus.focused != NULL){ |
| 68 | if (focus.loaded == false){ |
| 69 | loadProperties(); |
| 70 | setProperties(); |
| 71 | } |
| 72 | updateProperties(); |
| 73 | rControl->enableAppEvents(); |
| 74 | rControl->draw(); |
| 75 | mControl->enableAppEvents(); |
| 76 | mControl->draw(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | else {
|
| 81 | // Find out why resizeControl is being drawn elsewhere...
|
| 82 | rControl->disableAppEvents(); |
| 83 | mControl->disableAppEvents(); |
| 84 | clearProperties(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void EditorPanel::populate(){
|
| 89 | |
| 90 | label = editor->addTextField("Label:", 10, 30, 190, 20, &e_label); |
| 91 | |
| 92 | x = (editor->addTextField("X:", 10, 70, 40, 20, &e_x)); |
| 93 | y = (editor->addTextField("Y:", 60, 70, 40, 20, &e_y)); |
| 94 | w = (editor->addTextField("W:", 110, 70, 40, 20, &e_w)); |
| 95 | h = (editor->addTextField("H:", 160, 70, 40, 20, &e_h)); |
| 96 | |
| 97 | osc = (editor->addTextField("OSC Address", 10, 110, 190, 20, &e_osc)); |
| 98 | |
| 99 | editing = (editor->addButton("Editing", 20, (editor->height - 35), 80, 20, &stateManager::editing)); |
| 100 | editing->setToggleMode(true);
|
| 101 | cancel = (editor->addButton("Cancel", 110,(editor->height - 35), 80, 20, &e_cancel)); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | // Loads the editor's internal strings
|
| 106 | void EditorPanel::loadProperties(){
|
| 107 | |
| 108 | focus.loaded = true;
|
| 109 | |
| 110 | e_label = focus.focused->name; |
| 111 | e_x = toStr(focus.focused->x); |
| 112 | e_y = toStr(focus.focused->y); |
| 113 | e_w = toStr(focus.focused->width); |
| 114 | e_h = toStr(focus.focused->height); |
| 115 | e_osc = focus.focused->OSCaddress; |
| 116 | |
| 117 | } |
| 118 | // Sets the editors internal strings to the textboxes'
|
| 119 | void EditorPanel::setProperties(){
|
| 120 | |
| 121 | label->setInput(e_label); |
| 122 | x->setInput(e_x); |
| 123 | y->setInput(e_y); |
| 124 | w->setInput(e_w); |
| 125 | h->setInput(e_h); |
| 126 | osc->setInput(e_osc); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | // Updates control with value of local text boxes.
|
| 131 | void EditorPanel::updateProperties(){
|
| 132 | |
| 133 | // Input range checking?
|
| 134 | focus.focused->name = e_label; |
| 135 | focus.focused->x = atof(e_x.c_str()); |
| 136 | focus.focused->y = atof(e_y.c_str()); |
| 137 | focus.focused->width = atof(e_w.c_str()); |
| 138 | focus.focused->height = atof(e_h.c_str()); |
| 139 | focus.focused->OSCaddress = e_osc; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | void EditorPanel::clearProperties(){
|
| 144 | |
| 145 | e_label = "";
|
| 146 | e_x = "";
|
| 147 | e_y = "";
|
| 148 | e_w = "";
|
| 149 | e_h = "";
|
| 150 | e_osc = "";
|
| 151 | |
| 152 | label->setInput("No Focus");
|
| 153 | x->setInput("0");
|
| 154 | y->setInput("0");
|
| 155 | w->setInput("0");
|
| 156 | h->setInput("0");
|
| 157 | osc->setInput("");
|
| 158 | |
| 159 | } |
| 160 | |
| 161 | void EditorPanel::removeControl() {
|
| 162 | for (int i = 0; i <= gui->views[1]->controls.size() - 1; i++) { |
| 163 | if ( gui->views[1]->controls.at(i) == focus.focused){ |
| 164 | if (gui->views[1]->controls.size() >= 1){ |
| 165 | gui->views[1]->controls.erase(gui->views[1]->controls.begin()+i); |
| 166 | clearProperties(); |
| 167 | focus.clear(); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // TODO: Put this in a utility class:
|
| 174 | // Takes a number and outputs to string.
|
| 175 | string EditorPanel::toStr(float input){
|
| 176 | |
| 177 | // Clear the stream & reset.
|
| 178 | ostr.clear(); |
| 179 | ostr.str("");
|
| 180 | |
| 181 | // Put data to stream
|
| 182 | ostr << input; |
| 183 | |
| 184 | // Output as a string
|
| 185 | return ostr.str();
|
| 186 | } |
| 187 | |
| 188 | |
| 189 | // update INTERNAL numbers...
|
| 190 | void EditorPanel::updateX(int direction){ |
| 191 | if (stateManager::editing) {
|
| 192 | e_x = (direction == 1) ? toStr(atof(e_x.c_str()) - gridsize) : toStr(atof(e_x.c_str()) + gridsize);
|
| 193 | setProperties(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void EditorPanel::updateY(int direction){ |
| 198 | if (stateManager::editing) {
|
| 199 | e_y = (direction == 1) ? toStr(atof(e_y.c_str()) - gridsize) : toStr(atof(e_y.c_str()) + gridsize);
|
| 200 | setProperties(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void EditorPanel::updateSize(int width, int height){ |
| 205 | if (stateManager::editing) {
|
| 206 | e_w = toStr(atof(e_w.c_str()) + width); |
| 207 | e_h = toStr(atof(e_h.c_str()) + height); |
| 208 | setProperties(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void EditorPanel::updateLocation(int width, int height){ |
| 213 | if (stateManager::editing) {
|
| 214 | e_x = toStr(width); |
| 215 | e_y = toStr(height); |
| 216 | setProperties(); |
| 217 | } |
| 218 | } |
