From 87bb30d54eec27f3f3a39b0b4133875159122a64 Mon Sep 17 00:00:00 2001 From: gazer Date: Tue, 2 Feb 2016 13:22:47 +0300 Subject: [PATCH] small code enhancements --- src/dlangui/core/editable.d | 5 ++- src/dlangui/core/settings.d | 13 +++--- src/dlangui/core/types.d | 72 ++++++++++++++++------------------ src/dlangui/graphics/fonts.d | 37 +++++++---------- src/dlangui/graphics/ftfonts.d | 31 +++++++-------- 5 files changed, 70 insertions(+), 88 deletions(-) diff --git a/src/dlangui/core/editable.d b/src/dlangui/core/editable.d index a7186f3e..bc028fb8 100644 --- a/src/dlangui/core/editable.d +++ b/src/dlangui/core/editable.d @@ -1052,8 +1052,9 @@ class EditableContent { } /// inserts count empty lines at specified position - protected void insertLines(int start, int count) { - assert(count > 0); + protected void insertLines(int start, int count) + in { assert(count > 0); } + body { _lines.length += count; _tokenProps.length = _lines.length; _editMarks.length = _lines.length; diff --git a/src/dlangui/core/settings.d b/src/dlangui/core/settings.d index fd5dc262..79c6be35 100644 --- a/src/dlangui/core/settings.d +++ b/src/dlangui/core/settings.d @@ -29,7 +29,7 @@ module dlangui.core.settings; import dlangui.core.logger; import dlangui.core.types : parseHexDigit; import std.range; -import std.algorithm : equal; +import std.algorithm : clamp, equal; import std.conv : to; import std.utf : encode; import std.math : pow; @@ -82,15 +82,12 @@ class SettingsFile { } static int limitInt(long value, int minvalue, int maxvalue) { - if (value < minvalue) - return minvalue; - else if (value > maxvalue) - return maxvalue; - return cast(int)value; + return clamp(cast(int)value, minvalue, maxvalue); } - static string limitString(string value, const string[] values) { - assert(values.length > 0); + static string limitString(string value, const string[] values) + in { assert(values.length > 0); } + body { foreach(v; values) if (v.equal(value)) return value; diff --git a/src/dlangui/core/types.d b/src/dlangui/core/types.d index 8e0195d7..77e14fdd 100644 --- a/src/dlangui/core/types.d +++ b/src/dlangui/core/types.d @@ -121,12 +121,7 @@ struct Rect { return right <= left || bottom <= top; } /// translate rectangle coordinates by (x,y) - add deltax to x coordinates, and deltay to y coordinates - void moveBy(int deltax, int deltay) { - left += deltax; - right += deltax; - top += deltay; - bottom += deltay; - } + alias moveBy = offset; /// moves this rect to fit rc bounds, retaining the same size void moveToFit(ref Rect rc) { if (right > rc.right) @@ -175,6 +170,38 @@ struct Rect { } } + +/// widget state bit flags +enum State : uint { + /// state not specified / normal + Normal = 4 | 256, // Normal is Enabled + /// pressed (e.g. clicked by mouse) + Pressed = 1, + /// widget has focus + Focused = 2, + /// widget can process mouse and key events + Enabled = 4, + /// mouse pointer is over this widget + Hovered = 8, // mouse pointer is over control, buttons not pressed + /// widget is selected + Selected = 16, + /// widget can be checked + Checkable = 32, + /// widget is checked + Checked = 64, + /// widget is activated + Activated = 128, + /// window is focused + WindowFocused = 256, + /// widget is default control for form (should be focused when window gains focus first time) + Default = 512, // widget is default for form (e.g. default button will be focused on show) + /// widget has been focused by keyboard navigation + KeyboardFocused = 1024, + /// return state of parent instead of widget's state when requested + Parent = 0x10000, // use parent's state +} + + // Layout size constants /// layout option, to occupy all available place enum int FILL_PARENT = 0x4000_0000; @@ -418,6 +445,7 @@ struct Ref(T) { // if (T is RefCountedObject) } } + //================================================================================ // some utility functions @@ -441,38 +469,6 @@ wstring fromWStringz(const(wchar) * s) { return cast(wstring)(s[0..i].dup); } - - -/** widget state flags - bits */ -enum State : uint { - /// state not specified / normal - Normal = 4 | 256, // Normal is Enabled - /// pressed (e.g. clicked by mouse) - Pressed = 1, - /// widget has focus - Focused = 2, - /// widget can process mouse and key events - Enabled = 4, - /// mouse pointer is over this widget - Hovered = 8, // mouse pointer is over control, buttons not pressed - /// widget is selected - Selected = 16, - /// widget can be checked - Checkable = 32, - /// widget is checked - Checked = 64, - /// widget is activated - Activated = 128, - /// window is focused - WindowFocused = 256, - /// widget is default control for form (should be focused when window gains focus first time) - Default = 512, // widget is default for form (e.g. default button will be focused on show) - /// widget has been focused by keyboard navigation - KeyboardFocused = 1024, - /// return state of parent instead of widget's state when requested - Parent = 0x10000, // use parent's state -} - /** Deprecated: use std.uni.toUpper instead. Uppercase unicode character. */ diff --git a/src/dlangui/graphics/fonts.d b/src/dlangui/graphics/fonts.d index c8db5fa2..b73b1d93 100644 --- a/src/dlangui/graphics/fonts.d +++ b/src/dlangui/graphics/fonts.d @@ -84,12 +84,10 @@ immutable dchar UNICODE_NB_HYPHEN = 0x2011; struct CustomCharProps { uint color; uint textFlags; - this(uint color) { + + this(uint color, bool underline = false, bool strikeThrough = false) { this.color = color; this.textFlags = 0; - } - this(uint color, bool underline, bool strikeThrough = false) { - this.color = color; if (underline) this.textFlags |= TextFlag.Underline; if (strikeThrough) @@ -726,15 +724,12 @@ class FontManager { /// get font gamma (1.0 is neutral, < 1.0 makes glyphs lighter, >1.0 makes glyphs bolder) static @property double fontGamma() { return _fontGamma; } /// set font gamma (1.0 is neutral, < 1.0 makes glyphs lighter, >1.0 makes glyphs bolder) - static @property void fontGamma(double v) { - if (v < 0.1) - v = 0.1; - else if (v > 4) - v = 4; - if (_fontGamma != v) { - _fontGamma = v; - _gamma65.gamma = v; - _gamma256.gamma = v; + static @property void fontGamma(double v) { + double gamma = clamp(v, 0.1, 4); + if (_fontGamma != gamma) { + _fontGamma = gamma; + _gamma65.gamma = gamma; + _gamma256.gamma = gamma; if (_instance) _instance.clearGlyphCaches(); } @@ -767,7 +762,7 @@ struct GlyphCache private glyph_ptr[][1024] _glyphs; /// try to find glyph for character in cache, returns null if not found - Glyph * find(dchar ch) { + glyph_ptr find(dchar ch) { ch = ch & 0xF_FFFF; //if (_array is null) // _array = new Glyph[0x10000]; @@ -776,7 +771,7 @@ struct GlyphCache if (row is null) return null; uint i = ch & 0xFF; - Glyph * res = row[i]; + glyph_ptr res = row[i]; if (!res) return null; res.lastUsage = 1; @@ -784,7 +779,7 @@ struct GlyphCache } /// put character glyph to cache - Glyph * put(dchar ch, Glyph * glyph) { + glyph_ptr put(dchar ch, glyph_ptr glyph) { ch = ch & 0xF_FFFF; uint p = ch >> 8; uint i = ch & 0xFF; @@ -868,13 +863,9 @@ class glyph_gamma_table(int maxv = 65) { double v = (maxv - 1.0 - i) / maxv; v = pow(v, g); - int n = cast(int)round(v * 255); - n = 255 - n; - if (n < 0) - n = 0; - else if (n > 255) - n = 255; - _map[i] = cast(ubyte)n; + int n = 255 - cast(int)round(v * 255); + ubyte n_clamp = cast(ubyte)clamp(n, 0, 255); + _map[i] = n_clamp; } } /// correct byte value from source range to 0..255 applying gamma diff --git a/src/dlangui/graphics/ftfonts.d b/src/dlangui/graphics/ftfonts.d index 328539fd..421694b5 100644 --- a/src/dlangui/graphics/ftfonts.d +++ b/src/dlangui/graphics/ftfonts.d @@ -56,29 +56,26 @@ int stdFontFacePriority(string face) { //debug = FontResources; private struct FontDef { - immutable FontFamily _family; - immutable string _face; - immutable bool _italic; - immutable int _weight; - @property FontFamily family() { return _family; } - @property string face() { return _face; } - @property bool italic() { return _italic; } - @property int weight() { return _weight; } + immutable FontFamily family; + immutable string face; + immutable bool italic; + immutable int weight; + this(FontFamily family, string face, bool italic, int weight) { - _family = family; - _face = face; - _italic = italic; - _weight = weight; + this.family = family; + this.face = face; + this.italic = italic; + this.weight = weight; } bool opEquals(ref const FontDef v) const { - return _family == v._family && _italic == v._italic && _weight == v._weight && _face.equal(v._face); + return family == v.family && italic == v.italic && weight == v.weight && face.equal(v.face); } hash_t toHash() const nothrow @safe { hash_t res = 123; - res = res * 31 + cast(hash_t)_italic; - res = res * 31 + cast(hash_t)_weight; - res = res * 31 + cast(hash_t)_family; - res = res * 31 + typeid(_face).getHash(&_face); + res = res * 31 + cast(hash_t)italic; + res = res * 31 + cast(hash_t)weight; + res = res * 31 + cast(hash_t)family; + res = res * 31 + typeid(face).getHash(&face); return res; } }