PolyUIColorBox.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyUIColorBox.h"
  20. #include "PolyInputEvent.h"
  21. #include "PolyLabel.h"
  22. #include "PolyCoreServices.h"
  23. #include "PolyConfig.h"
  24. #include "PolyRenderer.h"
  25. using namespace Polycode;
  26. UIColorPicker::UIColorPicker() : UIWindow(L"", 300, 240) {
  27. closeOnEscape = true;
  28. continuous = true;
  29. // topPadding
  30. Config *conf = CoreServices::getInstance()->getConfig();
  31. Number uiScale = conf->getNumericValue("Polycode", "uiScale");
  32. String fontName = conf->getStringValue("Polycode", "uiDefaultFontName");
  33. int fontSize = conf->getNumericValue("Polycode", "uiDefaultFontSize");
  34. String mainBgImage = conf->getStringValue("Polycode", "uiColorPickerMainBg");
  35. String mainFrameImage = conf->getStringValue("Polycode", "uiColorPickerMainFrame");
  36. String hueFrameImage = conf->getStringValue("Polycode", "uiColorPickerHueFrame");
  37. String hueSelectorImage = conf->getStringValue("Polycode", "uiColorPickerHueSelector");
  38. String mainSelectorImage = conf->getStringValue("Polycode", "uiColorPickerMainSelector");
  39. mainBg = new UIImage(mainBgImage, 187, 187);
  40. mainBg->setPosition(padding, topPadding+padding);
  41. addChild(mainBg);
  42. mainFrame = new UIImage(mainFrameImage, 187, 187);
  43. mainFrame->setPosition(padding, topPadding+padding);
  44. alphaSlider = new UIHSlider(0, 1.0, mainFrame->getWidth());
  45. alphaSlider->setPosition(padding, mainFrame->getHeight() + mainFrame->getPosition().y + 13);
  46. addChild(alphaSlider);
  47. alphaSlider->addEventListener(this, UIEvent::CHANGE_EVENT);
  48. mainColorRect = new SceneMesh(Mesh::QUAD_MESH);
  49. mainColorRect->setBlendingMode(Renderer::BLEND_MODE_NORMAL);
  50. mainColorRect->setWidth(mainFrame->getWidth());
  51. mainColorRect->setHeight(mainFrame->getWidth());
  52. mainColorRect->setDepth(0.001);
  53. mainColorRect->getMesh()->addVertex(-mainFrame->getWidth()/2,mainFrame->getHeight()/2.0,0,0,0);
  54. mainColorRect->getMesh()->addVertex(mainFrame->getWidth()/2,mainFrame->getHeight()/2.0,0, 1, 0);
  55. mainColorRect->getMesh()->addVertex(mainFrame->getWidth()/2,-mainFrame->getHeight()/2.0,0, 1, 1);
  56. mainColorRect->getMesh()->addVertex(-mainFrame->getWidth()/2,-mainFrame->getHeight()/2.0,0,0,1);
  57. mainColorRect->getMesh()->dirtyArrays();
  58. mainColorRect->backfaceCulled = false;
  59. mainColorRect->setAnchorPoint(-1.0, -1.0, 0.0);
  60. mainColorRect->setPosition(padding, topPadding+padding);
  61. addChild(mainColorRect);
  62. addChild(mainFrame);
  63. hueFrame = new UIImage(hueFrameImage, 20, 187);
  64. hueFrame->setPosition(mainFrame->getPosition().x + mainFrame->getWidth()+10, topPadding+padding);
  65. addChild(hueFrame);
  66. hueSelector = new UIImage(hueSelectorImage);
  67. hueSelector->Resize(hueSelector->getWidth() / uiScale, hueSelector->getHeight() / uiScale);
  68. hueSelector->setAnchorPoint(0.0, 0.0, 0.0);
  69. hueSelector->setPosition(hueFrame->getPosition().x + (hueFrame->getWidth()/2.0), hueFrame->getPosition().y);
  70. addChild(hueSelector);
  71. hueSelector->setDragLimits(Polycode::Rectangle(hueSelector->getPosition().x,hueSelector->getPosition().y,0,hueFrame->getHeight()));
  72. mainSelector = new UIImage(mainSelectorImage);
  73. mainSelector->Resize(mainSelector->getWidth() / uiScale, mainSelector->getHeight() / uiScale);
  74. mainSelector->setAnchorPoint(0.0, 0.0, 0.0);
  75. mainSelector->setPosition(mainFrame->getPosition());
  76. addChild(mainSelector);
  77. mainColorRect->getMesh()->useVertexColors = true;
  78. mainColorRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  79. mainColorRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  80. mainColorRect->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  81. mainColorRect->processInputEvents = true;
  82. hueFrame->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  83. hueFrame->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  84. hueFrame->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  85. hueFrame->processInputEvents = true;
  86. UILabel *label = new UILabel(L"R:", fontSize, fontName);
  87. label->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 15, topPadding+padding + 3);
  88. addChild(label);
  89. junkLabels.push_back(label);
  90. rTextInput = new UITextInput(false, 40, 12);
  91. rTextInput->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 30, topPadding+padding);
  92. addChild(rTextInput);
  93. rTextInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  94. label = new UILabel(L"G:", fontSize, fontName);
  95. label->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 15, topPadding+padding + 33);
  96. addChild(label);
  97. junkLabels.push_back(label);
  98. gTextInput = new UITextInput(false, 40, 12);
  99. gTextInput->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 30, topPadding+padding + 30);
  100. addChild(gTextInput);
  101. gTextInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  102. label = new UILabel(L"B:", fontSize, fontName);
  103. label->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 15, topPadding+padding + 63);
  104. addChild(label);
  105. junkLabels.push_back(label);
  106. bTextInput = new UITextInput(false, 40, 12);
  107. bTextInput->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 30, topPadding+padding + 60);
  108. addChild(bTextInput);
  109. bTextInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  110. label = new UILabel(L"A:", fontSize, fontName);
  111. label->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 15, topPadding+padding + 93);
  112. addChild(label);
  113. junkLabels.push_back(label);
  114. aTextInput = new UITextInput(false, 40, 12);
  115. aTextInput->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 30, topPadding+padding + 90);
  116. addChild(aTextInput);
  117. aTextInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  118. setHue(0.0);
  119. setSaturationAndValue(0.0, 0.0);
  120. rTextInput->setNumberOnly(true);
  121. gTextInput->setNumberOnly(true);
  122. bTextInput->setNumberOnly(true);
  123. aTextInput->setNumberOnly(true);
  124. lastHueSelectorPosition = 0;
  125. mainSelector->setDragLimits(Polycode::Rectangle(mainColorRect->getPosition().x,mainColorRect->getPosition().y,mainColorRect->getWidth(), mainColorRect->getHeight()));
  126. colorAlpha = 1.0;
  127. visible = false;
  128. enabled = false;
  129. suppressTextChangeEvent = false;
  130. }
  131. void UIColorPicker::cancelColorListeners() {
  132. dispatchEvent(new Event(), Event::CANCEL_EVENT);
  133. }
  134. void UIColorPicker::onClose() {
  135. visible = false;
  136. enabled = false;
  137. }
  138. void UIColorPicker::rebuildFromTextInputs() {
  139. int newR = atoi(rTextInput->getText().c_str());
  140. int newG = atoi(gTextInput->getText().c_str());
  141. int newB = atoi(bTextInput->getText().c_str());
  142. int newA = atoi(aTextInput->getText().c_str());
  143. if(newR < 0)
  144. newR = 0;
  145. if(newR > 255)
  146. newR = 255;
  147. if(newG < 0)
  148. newG = 0;
  149. if(newG > 255)
  150. newG = 255;
  151. if(newB < 0)
  152. newB = 0;
  153. if(newB > 255)
  154. newB = 255;
  155. if(newA < 0)
  156. newA = 0;
  157. if(newA > 255)
  158. newA = 255;
  159. Color newColor;
  160. newColor.setColorRGBA(newR, newG, newB, newA);
  161. currentS = newColor.getSaturation();
  162. currentV = newColor.getValue();
  163. currentH = newColor.getHue();
  164. colorAlpha = newColor.a;
  165. updateSelectedColor(false);
  166. }
  167. bool UIColorPicker::isContinuous() {
  168. return continuous;
  169. }
  170. void UIColorPicker::setContinuous(bool _continuous) {
  171. continuous = _continuous;
  172. alphaSlider->setContinuous(continuous);
  173. }
  174. void UIColorPicker::setPickerColor(Color newColor) {
  175. currentS = newColor.getSaturation();
  176. currentV = newColor.getValue();
  177. currentH = newColor.getHue();
  178. colorAlpha = newColor.a;
  179. updateSelectedColor();
  180. }
  181. void UIColorPicker::setSaturationAndValue(Number S, Number V) {
  182. currentS = S;
  183. currentV = V;
  184. updateSelectedColor(true, false, false);
  185. }
  186. void UIColorPicker::updateSelectedColor(bool updateTextFields, bool updateHue, bool updateSV) {
  187. selectedColor.setColorHSV(currentH, currentS, currentV);
  188. selectedColor.a = colorAlpha;
  189. int newR = floor(selectedColor.r*255.0);
  190. int newG = floor(selectedColor.g*255.0);
  191. int newB = floor(selectedColor.b*255.0);
  192. int newA = floor(selectedColor.a*255.0);
  193. mainColorRect->color.a = colorAlpha;
  194. if(updateTextFields) {
  195. suppressTextChangeEvent = true;
  196. rTextInput->setText(String::IntToString(newR));
  197. suppressTextChangeEvent = true;
  198. gTextInput->setText(String::IntToString(newG));
  199. suppressTextChangeEvent = true;
  200. bTextInput->setText(String::IntToString(newB));
  201. suppressTextChangeEvent = true;
  202. aTextInput->setText(String::IntToString(newA));
  203. }
  204. Color hueCol;
  205. hueCol.setColorHSV(currentH, 1.0, 1.0);
  206. hueCol.a = colorAlpha;
  207. mainColorRect->getMesh()->getVertex(0)->vertexColor = Color((Number)1,(Number)1,(Number)1,colorAlpha);
  208. mainColorRect->getMesh()->getVertex(1)->vertexColor = hueCol;
  209. mainColorRect->getMesh()->getVertex(2)->vertexColor = Color((Number)0,(Number)0,(Number)0,colorAlpha);
  210. mainColorRect->getMesh()->getVertex(3)->vertexColor = Color((Number)0,(Number)0,(Number)0,colorAlpha);
  211. mainColorRect->getMesh()->arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
  212. if(updateHue) {
  213. hueSelector->setPositionY(hueFrame->getPosition().y + hueFrame->getHeight() - ((currentH/360.0) * hueFrame->getHeight()));
  214. lastHueSelectorPosition = hueSelector->getPosition().y;
  215. }
  216. if(updateSV) {
  217. mainSelector->setPosition(mainColorRect->getPosition().x + (currentS * mainColorRect->getWidth()), mainColorRect->getPosition().y + mainColorRect->getHeight() - (currentV * mainColorRect->getHeight()));
  218. lastMainSelectorPosition = mainSelector->getPosition2D();
  219. }
  220. alphaSlider->setSliderValue(colorAlpha);
  221. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  222. }
  223. Color UIColorPicker::getSelectedColor() {
  224. return selectedColor;
  225. }
  226. void UIColorPicker::handleEvent(Event *event) {
  227. if((event->getDispatcher() == rTextInput || event->getDispatcher() == gTextInput || event->getDispatcher() == bTextInput || event->getDispatcher() == aTextInput) && event->getEventType() == "UIEvent") {
  228. switch(event->getEventCode()) {
  229. case UIEvent::CHANGE_EVENT:
  230. {
  231. if(!suppressTextChangeEvent) {
  232. rebuildFromTextInputs();
  233. } else {
  234. suppressTextChangeEvent = false;
  235. }
  236. }
  237. break;
  238. }
  239. }
  240. if(event->getDispatcher() == alphaSlider) {
  241. switch(event->getEventCode()) {
  242. case UIEvent::CHANGE_EVENT:
  243. {
  244. colorAlpha = alphaSlider->getSliderValue();
  245. updateSelectedColor();
  246. }
  247. break;
  248. }
  249. }
  250. if(event->getDispatcher() == hueFrame) {
  251. switch(event->getEventCode()) {
  252. case InputEvent::EVENT_MOUSEDOWN:
  253. {
  254. InputEvent *inputEvent = (InputEvent*) event;
  255. hueSelector->setPositionY(inputEvent->getMousePosition().y+hueFrame->getPosition().y);
  256. hueSelector->startDrag(inputEvent->mousePosition.x-hueSelector->getPosition().x,inputEvent->mousePosition.y-hueSelector->getPosition().y+hueFrame->getPosition().y);
  257. }
  258. break;
  259. case InputEvent::EVENT_MOUSEUP:
  260. case InputEvent::EVENT_MOUSEUP_OUTSIDE:
  261. {
  262. hueSelector->stopDrag();
  263. updateColorFromHueSelector();
  264. }
  265. break;
  266. }
  267. }
  268. if(event->getDispatcher() == mainColorRect) {
  269. switch(event->getEventCode()) {
  270. case InputEvent::EVENT_MOUSEDOWN:
  271. {
  272. InputEvent *inputEvent = (InputEvent*) event;
  273. mainSelector->setPosition(inputEvent->getMousePosition().x+mainColorRect->getPosition().x, inputEvent->getMousePosition().y+mainColorRect->getPosition().y);
  274. mainSelector->startDrag(inputEvent->mousePosition.x-mainSelector->getPosition().x+mainColorRect->getPosition().x,inputEvent->mousePosition.y-mainSelector->getPosition().y+mainColorRect->getPosition().y);
  275. }
  276. break;
  277. case InputEvent::EVENT_MOUSEUP:
  278. case InputEvent::EVENT_MOUSEUP_OUTSIDE:
  279. {
  280. mainSelector->stopDrag();
  281. updateColorFromMainSelector();
  282. }
  283. break;
  284. }
  285. }
  286. UIWindow::handleEvent(event);
  287. }
  288. void UIColorPicker::setHue(Number hueNum) {
  289. if(hueNum < 0.0)
  290. hueNum = 0.0;
  291. if(hueNum >= 360.0)
  292. hueNum = 359.9999;
  293. currentH = hueNum;
  294. updateSelectedColor(true, false, false);
  295. }
  296. UIColorPicker::~UIColorPicker() {
  297. delete mainBg;
  298. delete mainFrame;
  299. delete alphaSlider;
  300. delete mainColorRect;
  301. delete hueFrame;
  302. delete hueSelector;
  303. delete mainSelector;
  304. delete rTextInput;
  305. delete gTextInput;
  306. delete bTextInput;
  307. delete aTextInput;
  308. for(int c = 0; c < junkLabels.size(); c++)
  309. delete junkLabels[c];
  310. }
  311. void UIColorPicker::updateColorFromMainSelector() {
  312. if(mainSelector->getPosition2D() != lastMainSelectorPosition) {
  313. lastMainSelectorPosition = mainSelector->getPosition2D();
  314. Number newPosX = lastMainSelectorPosition.x;
  315. Number newPosY = lastMainSelectorPosition.y;
  316. Number newV = 1.0 - (newPosY - mainColorRect->getPosition().y) / mainColorRect->getHeight();
  317. Number newS = (newPosX - mainColorRect->getPosition().x) / mainColorRect->getWidth();
  318. setSaturationAndValue(newS, newV);
  319. }
  320. }
  321. void UIColorPicker::updateColorFromHueSelector() {
  322. if(hueSelector->getPosition().y != lastHueSelectorPosition) {
  323. lastHueSelectorPosition = hueSelector->getPosition().y;
  324. Number newPosY = lastHueSelectorPosition;
  325. if(newPosY < hueFrame->getPosition().y)
  326. newPosY = hueFrame->getPosition().y;
  327. if(newPosY > hueFrame->getHeight() + hueFrame->getPosition().y)
  328. newPosY = hueFrame->getHeight() + hueFrame->getPosition().y;
  329. hueSelector->setPositionY(newPosY);
  330. Number newHue = 360.0 - (((newPosY-hueFrame->getPosition().y)/((hueFrame->getPosition().y+hueFrame->getHeight())-hueFrame->getPosition().y)) * 360.0f);
  331. setHue(newHue);
  332. }
  333. }
  334. void UIColorPicker::Update() {
  335. if (continuous) {
  336. if (mainSelector->isDragged())
  337. updateColorFromMainSelector();
  338. if (hueSelector->isDragged())
  339. updateColorFromHueSelector();
  340. }
  341. UIWindow::Update();
  342. }
  343. UIColorBox::UIColorBox(UIColorPicker *colorPicker, Color initialColor, Number width, Number height) : UIElement() {
  344. Config *conf = CoreServices::getInstance()->getConfig();
  345. String frameImageFile = conf->getStringValue("Polycode", "uiColorBoxFrameImage");
  346. String bgImageFile = conf->getStringValue("Polycode", "uiColorBoxBgImage");
  347. Number frameInset = conf->getNumericValue("Polycode", "uiColorBoxFrameInset");
  348. Number st = conf->getNumericValue("Polycode", "uiColorBoxFrameImageT");
  349. Number sr = conf->getNumericValue("Polycode", "uiColorBoxFrameImageR");
  350. Number sb = conf->getNumericValue("Polycode", "uiColorBoxFrameImageB");
  351. Number sl = conf->getNumericValue("Polycode", "uiColorBoxFrameImageL");
  352. bgImage = new UIRect(width-(frameInset*2), height-(frameInset*2));
  353. bgImage->setAnchorPoint(-1.0, -1.0, 0.0);
  354. bgImage->loadTexture(bgImageFile);
  355. bgImage->setPosition(frameInset, frameInset);
  356. addChild(bgImage);
  357. bgImage->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  358. bgImage->processInputEvents = true;
  359. colorShape = new UIRect(width-(frameInset*2), height-(frameInset*2));
  360. colorShape->setBlendingMode(Renderer::BLEND_MODE_NORMAL);
  361. colorShape->setAnchorPoint(-1.0, -1.0, 0.0);
  362. colorShape->setPosition(frameInset, frameInset);
  363. addChild(colorShape);
  364. frameImage = new UIBox(frameImageFile, st,sr,sb,sl, width, height);
  365. addChild(frameImage);
  366. this->colorPicker = colorPicker;
  367. colorPicker->addEventListener(this, Event::CHANGE_EVENT);
  368. colorPicker->addEventListener(this, Event::CANCEL_EVENT);
  369. setWidth(width);
  370. setHeight(height);
  371. selectedColor = initialColor;
  372. colorShape->color = selectedColor;
  373. listeningToPicker = false;
  374. }
  375. Color UIColorBox::getSelectedColor() {
  376. return selectedColor;
  377. }
  378. UIColorBox::~UIColorBox() {
  379. colorPicker->removeAllHandlersForListener(this);
  380. if(!ownsChildren) {
  381. delete bgImage;
  382. delete colorShape;
  383. delete frameImage;
  384. }
  385. }
  386. void UIColorBox::setBoxColor(Color newColor) {
  387. selectedColor = newColor;
  388. colorShape->color = selectedColor;
  389. if(listeningToPicker) {
  390. colorPicker->setPickerColor(newColor);
  391. }
  392. }
  393. void UIColorBox::showColorPicker() {
  394. colorPicker->visible = true;
  395. colorPicker->enabled = true;
  396. colorPicker->cancelColorListeners();
  397. colorPicker->setPickerColor(selectedColor);
  398. listeningToPicker = true;
  399. }
  400. void UIColorBox::handleEvent(Event *event) {
  401. if(event->getDispatcher() == colorPicker && event->getEventType() == "") {
  402. switch(event->getEventCode()) {
  403. case Event::CANCEL_EVENT:
  404. listeningToPicker = false;
  405. break;
  406. case Event::CHANGE_EVENT:
  407. if(listeningToPicker) {
  408. selectedColor = colorPicker->getSelectedColor();
  409. colorShape->color = selectedColor;
  410. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  411. }
  412. break;
  413. }
  414. }
  415. if(event->getDispatcher() == bgImage) {
  416. switch(event->getEventCode()) {
  417. case InputEvent::EVENT_MOUSEDOWN:
  418. showColorPicker();
  419. break;
  420. }
  421. }
  422. }