Merge pull request #177 from g4z3r/master

small code enhancements
This commit is contained in:
Vadim Lopatin 2016-02-03 11:34:44 +03:00
commit 81f068fc7b
5 changed files with 70 additions and 88 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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.
*/

View File

@ -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)
@ -727,14 +725,11 @@ class FontManager {
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;
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

View File

@ -34,29 +34,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;
}
}