Fixed Separator class spacing behavior
This commit is contained in:
@@ -10,12 +10,31 @@ namespace JUI {
|
||||
/// Dashed, dotted, and solid lines of varying thickness are supported.
|
||||
class Separator : public Widget {
|
||||
public:
|
||||
Separator();
|
||||
|
||||
explicit Separator(Widget* parent);
|
||||
|
||||
|
||||
|
||||
|
||||
/// Constructs a Separator widget by specifying it's parent. Additional style parameters can also be specified.
|
||||
/// @param parent
|
||||
/// @param orientation
|
||||
/// @param spacing
|
||||
/// @param line_mode
|
||||
/// @param line_color
|
||||
/// @param line_thickness
|
||||
Separator(Widget* parent,
|
||||
const enum Orientation& orientation = Orientation::Horizontal,
|
||||
const UDim& spacing = 5_px,
|
||||
const enum LineFillMode& line_mode = LineFillMode::Solid,
|
||||
const Color4& line_color = Colors::Black,
|
||||
float line_thickness = 1.f
|
||||
);
|
||||
|
||||
enum Orientation Orientation() const;
|
||||
|
||||
/// Sets whether the separator is laid out as a horizontal or vertical line.
|
||||
/// Horizontal orientation draws a horizontal line, for separating elements in a vertical list.
|
||||
/// Vertical orientation draws a vertical line, for separating elements in a horizontal list.
|
||||
void Orient(const enum Orientation& orientation);
|
||||
|
||||
enum LineFillMode LineFillMode() const;
|
||||
@@ -26,18 +45,43 @@ namespace JUI {
|
||||
|
||||
void LineColor(const Color4& color);
|
||||
|
||||
float GetAbsoluteSpacing() const;
|
||||
|
||||
|
||||
void InnerDraw() override;
|
||||
|
||||
float Spacing() const;
|
||||
//float Spacing() const;
|
||||
|
||||
void Spacing(float spacing);
|
||||
//void Spacing(float spacing);
|
||||
|
||||
/// @return The size of the separator object.
|
||||
UDim Spacing() const { return spacing;}
|
||||
/// Sets the size of the separator object.
|
||||
void Spacing(const UDim& value) {
|
||||
spacing = value;
|
||||
UpdateSize();
|
||||
}
|
||||
|
||||
/// @returns The spacing between
|
||||
float LineSpacing() const { return line_spacing;}
|
||||
void LineSpacing(float value) {
|
||||
line_spacing = value;
|
||||
}
|
||||
|
||||
protected:
|
||||
/// The default constructor zero-initializes all members.
|
||||
Separator();
|
||||
|
||||
/// Sets the widgets size based on the spacing and orientation.
|
||||
void UpdateSize();
|
||||
|
||||
enum LineFillMode line_mode = LineFillMode::Solid;
|
||||
enum Orientation orientation = Orientation::Horizontal;
|
||||
Color4 line_color = Colors::Black;
|
||||
float line_thickness = 1.f;
|
||||
float spacing = 2.f;
|
||||
float line_spacing = 2.f;
|
||||
|
||||
UDim spacing = 2_px;
|
||||
private:
|
||||
};
|
||||
|
||||
|
@@ -6,12 +6,31 @@ namespace JUI {
|
||||
Name("Separator");
|
||||
}
|
||||
|
||||
Separator::Separator(Widget *parent): Separator() {
|
||||
Parent(parent);
|
||||
void Separator::UpdateSize() {
|
||||
if (orientation == Orientation::Horizontal)
|
||||
Size({100_percent, spacing});
|
||||
|
||||
if (orientation == Orientation::Vertical)
|
||||
Size({spacing, 100_percent});
|
||||
}
|
||||
|
||||
Separator::Separator(Widget *parent, const enum JUI::Orientation &orientation, const UDim &spacing,
|
||||
const enum JUI::LineFillMode &line_mode, const Color4 &line_color, float line_thickness): Separator() {
|
||||
this->orientation = orientation;
|
||||
this->spacing = spacing;
|
||||
this->line_mode = line_mode;
|
||||
this->line_color = line_color;
|
||||
this->line_thickness = line_thickness;
|
||||
UpdateSize();
|
||||
Parent(parent);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Separator::Orient(const enum JUI::Orientation &orientation) {
|
||||
this->orientation = orientation;
|
||||
|
||||
UpdateSize();
|
||||
}
|
||||
|
||||
void Separator::LineFillMode(const enum JUI::LineFillMode &line_mode) {
|
||||
@@ -24,6 +43,24 @@ namespace JUI {
|
||||
line_color = color;
|
||||
}
|
||||
|
||||
float Separator::GetAbsoluteSpacing() const {
|
||||
if (parent == nullptr) {
|
||||
// TODO: This function should in theory not be called in this case.
|
||||
return 0.f;
|
||||
}
|
||||
auto abs_parent_size = parent->GetAbsoluteSize();
|
||||
if (orientation == ::JUI::Orientation::Horizontal) {
|
||||
return spacing.Pixels + (spacing.Scale * abs_parent_size.y);
|
||||
}
|
||||
|
||||
if (orientation == Orientation::Vertical) {
|
||||
return spacing.Pixels + (spacing.Scale * abs_parent_size.x);
|
||||
}
|
||||
|
||||
// This control path should never be reached.
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
void Separator::InnerDraw() {
|
||||
|
||||
Vector2 abs_pos = GetAbsolutePosition();
|
||||
@@ -34,6 +71,8 @@ namespace JUI {
|
||||
|
||||
// TODO: Factor in padding.
|
||||
|
||||
float spacing = GetAbsoluteSpacing();
|
||||
|
||||
if (orientation == Orientation::Horizontal) {
|
||||
start = abs_pos + Vector2(0, abs_size.y / 2.f);
|
||||
end = start + Vector2(abs_size.x, 0);
|
||||
@@ -44,22 +83,18 @@ namespace JUI {
|
||||
end = start + Vector2(0, abs_size.y);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (line_mode == LineFillMode::Dotted)
|
||||
J2D::DrawDottedLine(line_color, start, end, spacing, line_thickness);
|
||||
J2D::DrawDottedLine(line_color, start, end, line_spacing, line_thickness);
|
||||
|
||||
if (line_mode == LineFillMode::Dashed)
|
||||
J2D::DrawDashedLine(line_color, start, end ,spacing, line_thickness);
|
||||
J2D::DrawDashedLine(line_color, start, end, line_spacing, line_spacing, line_thickness);
|
||||
|
||||
if (line_mode == LineFillMode::Solid)
|
||||
JGL::J2D::DrawLine(line_color, start, end, line_thickness);
|
||||
}
|
||||
|
||||
float Separator::Spacing() const { return spacing; }
|
||||
|
||||
void Separator::Spacing(float spacing) {
|
||||
this->spacing = spacing;
|
||||
}
|
||||
|
||||
enum Orientation Separator::Orientation() const { return orientation;}
|
||||
|
||||
enum LineFillMode Separator::LineFillMode() const { return line_mode;}
|
||||
|
Reference in New Issue
Block a user