File: ColourPanel.cpp

package info (click to toggle)
mygui 3.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,224 kB
  • sloc: cpp: 118,031; ansic: 30,202; xml: 15,544; cs: 12,602; tcl: 776; python: 417; makefile: 34
file content (453 lines) | stat: -rw-r--r-- 13,560 bytes parent folder | download | duplicates (4)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*!
	@file
	@author		Albert Semenov
	@date		08/2008
*/

#include "Precompiled.h"
#include "ColourPanel.h"
#include "CommandManager.h"

namespace tools
{

	ColourPanel::ColourPanel() :
		mColourRect(nullptr),
		mColourView(nullptr),
		mImageColourPicker(nullptr),
		mEditRed(nullptr),
		mEditGreen(nullptr),
		mEditBlue(nullptr),
		mInputAlpha(nullptr),
		mTextAlpha(nullptr),
		mScrollRange(nullptr),
		mAlphaSliderBack(nullptr),
		mAlphaSliderPlace(nullptr),
		mAlphaSlider(nullptr),
		mTexture(nullptr),
		mAlphaSupport(true)
	{
	}

	ColourPanel::~ColourPanel()
	{
		destroyTexture();
	}

	void ColourPanel::OnInitialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName)
	{
		Control::OnInitialise(_parent, _place, GetLayoutName(this));

		InitialiseByAttributes(this);

		setDialogRoot(mMainWidget);

		mTextureName = MyGUI::utility::toString((size_t)this, "_ColourGradient");

		mCurrentColour = MyGUI::Colour::Green;
		mBaseColour = MyGUI::Colour::Green;

		mColourRect->eventMouseButtonPressed += MyGUI::newDelegate(this, &ColourPanel::notifyMouseButtonPressed);
		mColourRect->eventMouseDrag += MyGUI::newDelegate(this, &ColourPanel::notifyMouseDrag);
		mImageColourPicker->eventMouseDrag += MyGUI::newDelegate(this, &ColourPanel::notifyMouseDrag);
		mScrollRange->eventScrollChangePosition += MyGUI::newDelegate(this, &ColourPanel::notifyScrollChangePosition);
		mAlphaSlider->eventScrollChangePosition += MyGUI::newDelegate(this, &ColourPanel::notifyScrollChangePositionAlpha);

		mEditRed->eventEditTextChange += MyGUI::newDelegate(this, &ColourPanel::notifyEditTextChange);
		mEditGreen->eventEditTextChange += MyGUI::newDelegate(this, &ColourPanel::notifyEditTextChange);
		mEditBlue->eventEditTextChange += MyGUI::newDelegate(this, &ColourPanel::notifyEditTextChange);
		mInputAlpha->eventEditTextChange += MyGUI::newDelegate(this, &ColourPanel::notifyEditTextChangeAlpha);

		CommandManager::getInstance().getEvent("Command_ColorAccept")->connect(this, &ColourPanel::commandColorAccept);
		CommandManager::getInstance().getEvent("Command_ColorCancel")->connect(this, &ColourPanel::commandColorCancel);

		mColourRange.push_back(MyGUI::Colour(1, 0, 0));
		mColourRange.push_back(MyGUI::Colour(1, 0, 1));
		mColourRange.push_back(MyGUI::Colour(0, 0, 1));
		mColourRange.push_back(MyGUI::Colour(0, 1, 1));
		mColourRange.push_back(MyGUI::Colour(0, 1, 0));
		mColourRange.push_back(MyGUI::Colour(1, 1, 0));
		mColourRange.push_back(mColourRange[0]);

		mMainWidget->setVisible(false);

		createTexture();

		updateFirst();
	}

	void ColourPanel::updateFirst()
	{
		notifyScrollChangePosition(nullptr, mScrollRange->getScrollPosition());

		notifyMouseDrag(nullptr,
			mImageColourPicker->getAbsoluteLeft() + (mColourRect->getWidth() / 2),
			mImageColourPicker->getAbsoluteTop() + (mColourRect->getHeight() / 2),
			MyGUI::MouseButton::Left);
	}

	void ColourPanel::createTexture()
	{
		MyGUI::uint size = 32;
		mTexture = MyGUI::RenderManager::getInstance().createTexture(mTextureName);
		mTexture->createManual(size, size,
			MyGUI::TextureUsage::Static | MyGUI::TextureUsage::Write,
			MyGUI::PixelFormat::R8G8B8A8);

		mColourRect->setImageTexture(mTextureName);
	}

	void ColourPanel::destroyTexture()
	{
		MyGUI::RenderManager::getInstance().destroyTexture( mTexture );
		mTexture = nullptr;
	}

	void ColourPanel::updateTexture(const MyGUI::Colour& _colour)
	{
		size_t size = 32;

		MyGUI::uint8* pDest = static_cast<MyGUI::uint8*>(mTexture->lock(MyGUI::TextureUsage::Write));

		for (size_t j = 0; j < size; j++)
		{
			for (size_t i = 0; i < size; i++)
			{
				float x = (float)i / size;
				float y = (float)j / size;
				*pDest++ = MyGUI::uint8((1. - y) * (_colour.blue  * x + (1. - x)) * 255); // B
				*pDest++ = MyGUI::uint8((1. - y) * (_colour.green * x + (1. - x)) * 255); // G
				*pDest++ = MyGUI::uint8((1. - y) * (_colour.red   * x + (1. - x)) * 255); // R
				*pDest++ = 255; // A
			}
		}

		// Unlock the pixel buffer
		mTexture->unlock();
	}

	void ColourPanel::notifyMouseDrag(MyGUI::Widget* _sender, int _left, int _top, MyGUI::MouseButton _id)
	{
		if (_id != MyGUI::MouseButton::Left)
			return;

		MyGUI::Widget* parent = mImageColourPicker->getParent();
		MyGUI::IntPoint point(_left - parent->getAbsoluteLeft(), _top - parent->getAbsoluteTop());

		if (point.left < 0)
			point.left = 0;
		if (point.top < 0)
			point.top = 0;
		if (point.left > mColourRect->getWidth())
			point.left = mColourRect->getWidth();
		if (point.top > mColourRect->getHeight())
			point.top = mColourRect->getHeight();

		mImageColourPicker->setPosition(point.left - (mImageColourPicker->getWidth() / 2), point.top - (mImageColourPicker->getHeight() / 2));

		updateFromPoint(point);
	}

	void ColourPanel::notifyMouseButtonPressed(MyGUI::Widget* _sender, int _left, int _top, MyGUI::MouseButton _id)
	{
		if (_id == MyGUI::MouseButton::Left)
			notifyMouseDrag(nullptr, _left, _top, _id);
	}

	void ColourPanel::updateFromPoint(const MyGUI::IntPoint& _point)
	{
		// get colour by cursor position Altren 09.2008
		float x = 1.0f * _point.left / mColourRect->getWidth();
		float y = 1.0f * _point.top / mColourRect->getHeight();

		if (x > 1)
			x = 1;
		else if (x < 0)
			x = 0;

		if (y > 1)
			y = 1;
		else if (y < 0)
			y = 0;

		mCurrentColour.red = (1 - y) * (mBaseColour.red * x + MyGUI::Colour::White.red * (1 - x));
		mCurrentColour.green = (1 - y) * (mBaseColour.green * x + MyGUI::Colour::White.green * (1 - x));
		mCurrentColour.blue = (1 - y) * (mBaseColour.blue * x + MyGUI::Colour::White.blue * (1 - x));

		mColourView->setColour(mCurrentColour);
		mAlphaSliderBack->setColour(mCurrentColour);

		eventPreviewColour(mCurrentColour);

		mEditRed->setCaption(MyGUI::utility::toString((int)(mCurrentColour.red * 255)));
		mEditGreen->setCaption(MyGUI::utility::toString((int)(mCurrentColour.green * 255)));
		mEditBlue->setCaption(MyGUI::utility::toString((int)(mCurrentColour.blue * 255)));
	}

	void ColourPanel::notifyScrollChangePosition(MyGUI::ScrollBar* _sender, size_t _position)
	{
		float sector_size = (float)mScrollRange->getScrollRange() / 6.0f;
		float sector_current = (float)_position / sector_size;

		// current sector
		size_t current = (size_t)sector_current;
		assert(current < 6);
		// offset to the next sector from 0 to 1
		float offfset = (sector_current - (float)current);

		const MyGUI::Colour& from = mColourRange[current];
		const MyGUI::Colour& to = mColourRange[current + 1];

		mBaseColour.red = from.red + offfset * (to.red - from.red);
		mBaseColour.green = from.green + offfset * (to.green - from.green);
		mBaseColour.blue = from.blue + offfset * (to.blue - from.blue);

		updateTexture(mBaseColour);

		MyGUI::IntPoint point(
			mImageColourPicker->getLeft() + (mImageColourPicker->getWidth() / 2),
			mImageColourPicker->getTop() + (mImageColourPicker->getHeight() / 2));

		updateFromPoint(point);
	}

	void ColourPanel::notifyEditTextChange(MyGUI::EditBox* _sender)
	{
		MyGUI::EditBox* edit = static_cast<MyGUI::EditBox*>(_sender);
		size_t cursor = edit->getTextCursor();
		size_t num = MyGUI::utility::parseSizeT(edit->getOnlyText());
		if (num > 255)
			num = 255;
		edit->setCaption(MyGUI::utility::toString(num));
		if (cursor < edit->getTextLength())
			edit->setTextCursor(cursor);

		MyGUI::Colour colour(
			MyGUI::utility::parseFloat(mEditRed->getOnlyText()) / 255.0f,
			MyGUI::utility::parseFloat(mEditGreen->getOnlyText()) / 255.0f,
			MyGUI::utility::parseFloat(mEditBlue->getOnlyText()) / 255.0f);

		updateFromColour(colour);
	}

	void ColourPanel::setColour(const MyGUI::Colour& _colour)
	{
		MyGUI::Colour colour = getSaturate(_colour);
		mEditRed->setCaption(MyGUI::utility::toString((int)(colour.red * 255)));
		mEditGreen->setCaption(MyGUI::utility::toString((int)(colour.green * 255)));
		mEditBlue->setCaption(MyGUI::utility::toString((int)(colour.blue * 255)));
		mInputAlpha->setCaption(MyGUI::utility::toString(mAlphaSupport ? colour.alpha : 1));

		updateFromColour(colour);
	}

	void ColourPanel::updateFromColour(const MyGUI::Colour& _colour)
	{
		mCurrentColour = _colour;
		if (!mAlphaSupport)
			mCurrentColour.alpha = 1;

		std::vector<float> vec;
		vec.push_back(mCurrentColour.red);
		vec.push_back(mCurrentColour.green);
		vec.push_back(mCurrentColour.blue);
		std::sort(vec.begin(), vec.end());

		MyGUI::IntPoint point((int)((1 - vec[0] / vec[2]) * mColourRect->getWidth()), (int)((1 - vec[2]) * mColourRect->getHeight()));
		mImageColourPicker->setPosition(point.left - (mImageColourPicker->getWidth() / 2), point.top - (mImageColourPicker->getHeight() / 2));

		int iMax = (mCurrentColour.red == vec[2]) ? 0 : (mCurrentColour.green == vec[2]) ? 1 : 2;
		int iMin = (mCurrentColour.red == vec[0]) ? 0 : (mCurrentColour.green == vec[0]) ? 1 : 2;
		int iAvg = 3 - iMax - iMin;

		if (iMin == iMax) // if gray colour - set base red
		{
			iMax = 0;
			iMin = 1;
			iAvg = 2;
			byIndex(mBaseColour, iMin) = 0.;
			byIndex(mBaseColour, iAvg) = 0.;
			byIndex(mBaseColour, iMax) = 1.;
		}
		else
		{
			byIndex(mBaseColour, iMin) = 0.;
			byIndex(mBaseColour, iAvg) = (vec[1] - vec[0]) / (vec[2] - vec[0]);
			byIndex(mBaseColour, iMax) = 1.;
		}

		int i = 0;
		for (i = 0; i < 6; ++i)
		{
			if ((fabs(byIndex(mColourRange[i], iMin) - byIndex(mBaseColour, iMin)) < 0.001) &&
				(fabs(byIndex(mColourRange[i], iMax) - byIndex(mBaseColour, iMax)) < 0.001) &&
				(fabs(byIndex(mColourRange[i+1], iMin) - byIndex(mBaseColour, iMin)) < 0.001) &&
				(fabs(byIndex(mColourRange[i+1], iMax) - byIndex(mBaseColour, iMax)) < 0.001))
				break;
		}

		float sector_size = (float)mScrollRange->getScrollRange() / 6.0f;
		size_t current = i;

		float offset = byIndex(mBaseColour, iAvg);
		if (byIndex(mColourRange[i+1], iAvg) < byIndex(mColourRange[i], iAvg))
			offset = 1 - byIndex(mBaseColour, iAvg);

		size_t pos = size_t((current + offset) * sector_size);

		mScrollRange->setScrollPosition(pos);

		// bonus for colour's cutoff under scale
		mBaseColour.red = mColourRange[i].red + offset * (mColourRange[i+1].red - mColourRange[i].red);
		mBaseColour.green = mColourRange[i].green + offset * (mColourRange[i+1].green - mColourRange[i].green);
		mBaseColour.blue = mColourRange[i].blue + offset * (mColourRange[i+1].blue - mColourRange[i].blue);

		updateTexture(mBaseColour);

		mAlphaSlider->setScrollPosition((size_t)((double)(mAlphaSlider->getScrollRange() - 1) * (double)mCurrentColour.alpha));

		mColourView->setColour(mCurrentColour);
		mColourView->setAlpha(mCurrentColour.alpha);
		mAlphaSliderBack->setColour(mCurrentColour);

		eventPreviewColour(mCurrentColour);
	}

	MyGUI::Colour ColourPanel::getSaturate(const MyGUI::Colour& _colour) const
	{
		MyGUI::Colour colour = _colour;
		if (colour.red < 0)
			colour.red = 0;
		else if (colour.red > 1)
			colour.red = 1;

		if (colour.green < 0)
			colour.green = 0;
		else if (colour.green > 1)
			colour.green = 1;

		if (colour.blue < 0)
			colour.blue = 0;
		else if (colour.blue > 1)
			colour.blue = 1;

		return colour;
	}

	float& ColourPanel::byIndex(MyGUI::Colour& _colour, size_t _index)
	{
		if (_index == 0)
			return _colour.red;
		else if (_index == 1)
			return _colour.green;
		else if (_index == 2)
			return _colour.blue;
		else
			return _colour.alpha;
	}

	void ColourPanel::onDoModal()
	{
	}

	void ColourPanel::onEndModal()
	{
	}

	const MyGUI::Colour& ColourPanel::getColour() const
	{
		return mCurrentColour;
	}

	void ColourPanel::notifyEditTextChangeAlpha(MyGUI::EditBox* _sender)
	{
		MyGUI::UString value = _sender->getOnlyText();

		mCurrentColour.alpha = MyGUI::utility::parseValue<float>(value);

		bool validate = true;
		if (mCurrentColour.alpha > 1)
		{
			mCurrentColour.alpha = 1;
			validate = false;
		}
		else if (mCurrentColour.alpha < 0)
		{
			mCurrentColour.alpha = 0;
			validate = false;
		}

		if (!validate)
			value = MyGUI::utility::toString(mCurrentColour.alpha);

		size_t index = _sender->getTextCursor();
		_sender->setCaption(value);
		_sender->setTextCursor(index);

		mAlphaSlider->setScrollPosition((size_t)((double)(mAlphaSlider->getScrollRange() - 1) * (double)mCurrentColour.alpha));
		mColourView->setAlpha(mCurrentColour.alpha);

		eventPreviewColour(mCurrentColour);
	}

	void ColourPanel::notifyScrollChangePositionAlpha(MyGUI::ScrollBar* _sender, size_t _position)
	{
		mCurrentColour.alpha = (float)((double)mAlphaSlider->getScrollPosition() / (double)(mAlphaSlider->getScrollRange() - 1));

		if (mCurrentColour.alpha > 1)
			mCurrentColour.alpha = 1;
		else if (mCurrentColour.alpha < 0)
			mCurrentColour.alpha = 0;

		mInputAlpha->setCaption(MyGUI::utility::toString(mCurrentColour.alpha));
		mColourView->setAlpha(mCurrentColour.alpha);

		eventPreviewColour(mCurrentColour);
	}

	void ColourPanel::setAlphaSupport(bool _value)
	{
		mAlphaSupport = _value;
		updateAlphaSupport();

		setColour(getColour());
	}

	bool ColourPanel::getAlphaSupport() const
	{
		return mAlphaSupport;
	}

	void ColourPanel::updateAlphaSupport()
	{
		mInputAlpha->setVisible(mAlphaSupport);
		mTextAlpha->setVisible(mAlphaSupport);
		mAlphaSliderPlace->setVisible(mAlphaSupport);
	}

	bool ColourPanel::checkCommand()
	{
		return isDialogModal();
	}

	void ColourPanel::commandColorAccept(const MyGUI::UString& _commandName, bool& _result)
	{
		if (!checkCommand())
			return;

		eventEndDialog(this, true);

		_result = true;
	}

	void ColourPanel::commandColorCancel(const MyGUI::UString& _commandName, bool& _result)
	{
		if (!checkCommand())
			return;

		eventEndDialog(this, false);

		_result = true;
	}

}