-
Notifications
You must be signed in to change notification settings - Fork 314
/
Element.cpp
2846 lines (2351 loc) · 81 KB
/
Element.cpp
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019-2023 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "../../Include/RmlUi/Core/Element.h"
#include "../../Include/RmlUi/Core/Context.h"
#include "../../Include/RmlUi/Core/Core.h"
#include "../../Include/RmlUi/Core/Dictionary.h"
#include "../../Include/RmlUi/Core/ElementDocument.h"
#include "../../Include/RmlUi/Core/ElementInstancer.h"
#include "../../Include/RmlUi/Core/ElementScroll.h"
#include "../../Include/RmlUi/Core/ElementUtilities.h"
#include "../../Include/RmlUi/Core/Factory.h"
#include "../../Include/RmlUi/Core/Profiling.h"
#include "../../Include/RmlUi/Core/PropertiesIteratorView.h"
#include "../../Include/RmlUi/Core/PropertyDefinition.h"
#include "../../Include/RmlUi/Core/PropertyIdSet.h"
#include "../../Include/RmlUi/Core/StyleSheet.h"
#include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
#include "../../Include/RmlUi/Core/TransformPrimitive.h"
#include "Clock.h"
#include "ComputeProperty.h"
#include "DataModel.h"
#include "ElementAnimation.h"
#include "ElementBackgroundBorder.h"
#include "ElementDecoration.h"
#include "ElementDefinition.h"
#include "ElementStyle.h"
#include "EventDispatcher.h"
#include "EventSpecification.h"
#include "Layout/LayoutEngine.h"
#include "PluginRegistry.h"
#include "Pool.h"
#include "PropertiesIterator.h"
#include "StyleSheetNode.h"
#include "StyleSheetParser.h"
#include "TransformState.h"
#include "TransformUtilities.h"
#include "XMLParseTools.h"
#include <algorithm>
#include <cmath>
namespace Rml {
// Determines how many levels up in the hierarchy the OnChildAdd and OnChildRemove are called (starting at the child itself)
static constexpr int ChildNotifyLevels = 2;
// Helper function to select scroll offset delta
static float GetScrollOffsetDelta(ScrollAlignment alignment, float begin_offset, float end_offset)
{
switch (alignment)
{
case ScrollAlignment::Start: return begin_offset;
case ScrollAlignment::Center: return (begin_offset + end_offset) / 2.0f;
case ScrollAlignment::End: return end_offset;
case ScrollAlignment::Nearest:
if (begin_offset >= 0.f && end_offset <= 0.f)
return 0.f; // Element is already visible, don't scroll
else if (begin_offset < 0.f && end_offset < 0.f)
return Math::Max(begin_offset, end_offset);
else if (begin_offset > 0.f && end_offset > 0.f)
return Math::Min(begin_offset, end_offset);
else
return 0.f; // Shouldn't happen
}
return 0.f;
}
// Meta objects for element collected in a single struct to reduce memory allocations
struct ElementMeta {
ElementMeta(Element* el) : event_dispatcher(el), style(el), background_border(), decoration(el), scroll(el), computed_values(el) {}
SmallUnorderedMap<EventId, EventListener*> attribute_event_listeners;
EventDispatcher event_dispatcher;
ElementStyle style;
ElementBackgroundBorder background_border;
ElementDecoration decoration;
ElementScroll scroll;
Style::ComputedValues computed_values;
};
static Pool<ElementMeta> element_meta_chunk_pool(200, true);
Element::Element(const String& tag) :
local_stacking_context(false), local_stacking_context_forced(false), stacking_context_dirty(false), computed_values_are_default_initialized(true),
visible(true), offset_fixed(false), absolute_offset_dirty(true), dirty_definition(false), dirty_child_definitions(false), dirty_animation(false),
dirty_transition(false), dirty_transform(false), dirty_perspective(false), tag(tag), relative_offset_base(0, 0), relative_offset_position(0, 0),
absolute_offset(0, 0), scroll_offset(0, 0)
{
RMLUI_ASSERT(tag == StringUtilities::ToLower(tag));
parent = nullptr;
focus = nullptr;
instancer = nullptr;
owner_document = nullptr;
offset_parent = nullptr;
client_area = BoxArea::Padding;
baseline = 0.0f;
num_non_dom_children = 0;
z_index = 0;
meta = element_meta_chunk_pool.AllocateAndConstruct(this);
data_model = nullptr;
}
Element::~Element()
{
RMLUI_ASSERT(parent == nullptr);
PluginRegistry::NotifyElementDestroy(this);
// A simplified version of RemoveChild() for destruction.
for (ElementPtr& child : children)
{
Element* child_ancestor = child.get();
for (int i = 0; i <= ChildNotifyLevels && child_ancestor; i++, child_ancestor = child_ancestor->GetParentNode())
child_ancestor->OnChildRemove(child.get());
child->SetParent(nullptr);
}
children.clear();
num_non_dom_children = 0;
element_meta_chunk_pool.DestroyAndDeallocate(meta);
}
void Element::Update(float dp_ratio, Vector2f vp_dimensions)
{
#ifdef RMLUI_ENABLE_PROFILING
auto name = GetAddress(false, false);
RMLUI_ZoneScoped;
RMLUI_ZoneText(name.c_str(), name.size());
#endif
OnUpdate();
HandleTransitionProperty();
HandleAnimationProperty();
AdvanceAnimations();
meta->scroll.Update();
UpdateProperties(dp_ratio, vp_dimensions);
// Do en extra pass over the animations and properties if the 'animation' property was just changed.
if (dirty_animation)
{
HandleAnimationProperty();
AdvanceAnimations();
UpdateProperties(dp_ratio, vp_dimensions);
}
meta->decoration.InstanceDecorators();
for (size_t i = 0; i < children.size(); i++)
children[i]->Update(dp_ratio, vp_dimensions);
if (!animations.empty() && IsVisible(true))
{
if (Context* ctx = GetContext())
ctx->RequestNextUpdate(0);
}
}
void Element::UpdateProperties(const float dp_ratio, const Vector2f vp_dimensions)
{
UpdateDefinition();
if (meta->style.AnyPropertiesDirty())
{
const ComputedValues* parent_values = parent ? &parent->GetComputedValues() : nullptr;
const ComputedValues* document_values = owner_document ? &owner_document->GetComputedValues() : nullptr;
// Compute values and clear dirty properties
PropertyIdSet dirty_properties = meta->style.ComputeValues(meta->computed_values, parent_values, document_values,
computed_values_are_default_initialized, dp_ratio, vp_dimensions);
computed_values_are_default_initialized = false;
// Computed values are just calculated and can safely be used in OnPropertyChange.
// However, new properties set during this call will not be available until the next update loop.
if (!dirty_properties.Empty())
OnPropertyChange(dirty_properties);
}
}
void Element::Render()
{
#ifdef RMLUI_ENABLE_PROFILING
auto name = GetAddress(false, false);
RMLUI_ZoneScoped;
RMLUI_ZoneText(name.c_str(), name.size());
#endif
// TODO: This is a work-around for the dirty offset not being properly updated when used by containing block children. This results
// in scrolling not working properly. We don't care about the return value, the call is only used to force the absolute offset to update.
if (absolute_offset_dirty)
GetAbsoluteOffset(BoxArea::Border);
// Rebuild our stacking context if necessary.
if (stacking_context_dirty)
BuildLocalStackingContext();
UpdateTransformState();
// Apply our transform
ElementUtilities::ApplyTransform(*this);
// Set up the clipping region for this element.
if (ElementUtilities::SetClippingRegion(this))
{
meta->background_border.Render(this);
meta->decoration.RenderDecorators();
{
RMLUI_ZoneScopedNC("OnRender", 0x228B22);
OnRender();
}
}
// Render all elements in our local stacking context.
for (Element* element : stacking_context)
element->Render();
}
ElementPtr Element::Clone() const
{
ElementPtr clone;
if (instancer)
{
clone = instancer->InstanceElement(nullptr, GetTagName(), attributes);
if (clone)
clone->SetInstancer(instancer);
}
else
clone = Factory::InstanceElement(nullptr, GetTagName(), GetTagName(), attributes);
if (clone)
{
// Copy over the attributes. The 'style' and 'class' attributes are skipped because inline styles and class names are copied manually below.
// This is necessary in case any properties or classes have been set manually, in which case the 'style' and 'class' attributes are out of
// sync with the used style and active classes.
ElementAttributes clone_attributes = attributes;
clone_attributes.erase("style");
clone_attributes.erase("class");
clone->SetAttributes(clone_attributes);
for (auto& id_property : GetStyle()->GetLocalStyleProperties())
clone->SetProperty(id_property.first, id_property.second);
clone->GetStyle()->SetClassNames(GetStyle()->GetClassNames());
String inner_rml;
GetInnerRML(inner_rml);
clone->SetInnerRML(inner_rml);
}
return clone;
}
void Element::SetClass(const String& class_name, bool activate)
{
if (meta->style.SetClass(class_name, activate))
DirtyDefinition(DirtyNodes::SelfAndSiblings);
}
bool Element::IsClassSet(const String& class_name) const
{
return meta->style.IsClassSet(class_name);
}
void Element::SetClassNames(const String& class_names)
{
SetAttribute("class", class_names);
}
String Element::GetClassNames() const
{
return meta->style.GetClassNames();
}
const StyleSheet* Element::GetStyleSheet() const
{
if (ElementDocument* document = GetOwnerDocument())
return document->GetStyleSheet();
return nullptr;
}
String Element::GetAddress(bool include_pseudo_classes, bool include_parents) const
{
// Add the tag name onto the address.
String address(tag);
// Add the ID if we have one.
if (!id.empty())
{
address += "#";
address += id;
}
String classes = meta->style.GetClassNames();
if (!classes.empty())
{
classes = StringUtilities::Replace(classes, ' ', '.');
address += ".";
address += classes;
}
if (include_pseudo_classes)
{
const PseudoClassMap& pseudo_classes = meta->style.GetActivePseudoClasses();
for (auto& pseudo_class : pseudo_classes)
{
address += ":";
address += pseudo_class.first;
}
}
if (include_parents && parent)
{
address += " < ";
return address + parent->GetAddress(include_pseudo_classes, true);
}
else
return address;
}
void Element::SetOffset(Vector2f offset, Element* _offset_parent, bool _offset_fixed)
{
_offset_fixed |= GetPosition() == Style::Position::Fixed;
// If our offset has definitely changed, or any of our parenting has, then these are set and
// updated based on our left / right / top / bottom properties.
if (relative_offset_base != offset || offset_parent != _offset_parent || offset_fixed != _offset_fixed)
{
relative_offset_base = offset;
offset_fixed = _offset_fixed;
offset_parent = _offset_parent;
UpdateOffset();
DirtyAbsoluteOffset();
}
// Otherwise, our offset is updated in case left / right / top / bottom will have an impact on
// our final position, and our children are dirtied if they do.
else
{
const Vector2f old_base = relative_offset_base;
const Vector2f old_position = relative_offset_position;
UpdateOffset();
if (old_base != relative_offset_base || old_position != relative_offset_position)
DirtyAbsoluteOffset();
}
}
Vector2f Element::GetRelativeOffset(BoxArea area)
{
return relative_offset_base + relative_offset_position + GetBox().GetPosition(area);
}
Vector2f Element::GetAbsoluteOffset(BoxArea area)
{
if (absolute_offset_dirty)
{
absolute_offset_dirty = false;
if (offset_parent)
absolute_offset = offset_parent->GetAbsoluteOffset(BoxArea::Border) + relative_offset_base + relative_offset_position;
else
absolute_offset = relative_offset_base + relative_offset_position;
if (!offset_fixed)
{
// Add any parent scrolling onto our position as well.
if (offset_parent)
absolute_offset -= offset_parent->scroll_offset;
// Finally, there may be relatively positioned elements between ourself and our containing block, add their relative offsets as well.
for (Element* ancestor = parent; ancestor && ancestor != offset_parent; ancestor = ancestor->parent)
absolute_offset += ancestor->relative_offset_position;
}
}
return absolute_offset + GetBox().GetPosition(area);
}
void Element::SetClientArea(BoxArea _client_area)
{
client_area = _client_area;
}
BoxArea Element::GetClientArea() const
{
return client_area;
}
void Element::SetScrollableOverflowRectangle(Vector2f _scrollable_overflow_rectangle)
{
if (scrollable_overflow_rectangle != _scrollable_overflow_rectangle)
{
scrollable_overflow_rectangle = _scrollable_overflow_rectangle;
scroll_offset.x = Math::Min(scroll_offset.x, GetScrollWidth() - GetClientWidth());
scroll_offset.y = Math::Min(scroll_offset.y, GetScrollHeight() - GetClientHeight());
DirtyAbsoluteOffset();
}
}
void Element::SetBox(const Box& box)
{
if (box != main_box || additional_boxes.size() > 0)
{
main_box = box;
additional_boxes.clear();
OnResize();
meta->background_border.DirtyBackground();
meta->background_border.DirtyBorder();
meta->decoration.DirtyDecoratorsData();
}
}
void Element::AddBox(const Box& box, Vector2f offset)
{
additional_boxes.emplace_back(PositionedBox{box, offset});
OnResize();
meta->background_border.DirtyBackground();
meta->background_border.DirtyBorder();
meta->decoration.DirtyDecoratorsData();
}
const Box& Element::GetBox()
{
return main_box;
}
const Box& Element::GetBox(int index, Vector2f& offset)
{
offset = Vector2f(0);
if (index < 1)
return main_box;
const int additional_box_index = index - 1;
if (additional_box_index >= (int)additional_boxes.size())
return main_box;
offset = additional_boxes[additional_box_index].offset;
return additional_boxes[additional_box_index].box;
}
int Element::GetNumBoxes()
{
return 1 + (int)additional_boxes.size();
}
float Element::GetBaseline() const
{
return baseline;
}
bool Element::GetIntrinsicDimensions(Vector2f& /*dimensions*/, float& /*ratio*/)
{
return false;
}
bool Element::IsReplaced()
{
Vector2f unused_dimensions;
float unused_ratio = 0.f;
return GetIntrinsicDimensions(unused_dimensions, unused_ratio);
}
bool Element::IsPointWithinElement(const Vector2f point)
{
const Vector2f position = GetAbsoluteOffset(BoxArea::Border);
for (int i = 0; i < GetNumBoxes(); ++i)
{
Vector2f box_offset;
const Box& box = GetBox(i, box_offset);
const Vector2f box_position = position + box_offset;
const Vector2f box_dimensions = box.GetSize(BoxArea::Border);
if (point.x >= box_position.x && point.x <= (box_position.x + box_dimensions.x) && point.y >= box_position.y &&
point.y <= (box_position.y + box_dimensions.y))
{
return true;
}
}
return false;
}
bool Element::IsVisible(bool include_ancestors) const
{
if (!include_ancestors)
return visible;
const Element* element = this;
while (element)
{
if (!element->visible)
return false;
element = element->parent;
}
return true;
}
float Element::GetZIndex() const
{
return z_index;
}
FontFaceHandle Element::GetFontFaceHandle() const
{
return meta->computed_values.font_face_handle();
}
bool Element::SetProperty(const String& name, const String& value)
{
// The name may be a shorthand giving us multiple underlying properties
PropertyDictionary properties;
if (!StyleSheetSpecification::ParsePropertyDeclaration(properties, name, value))
{
Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.c_str(), value.c_str());
return false;
}
for (auto& property : properties.GetProperties())
{
if (!meta->style.SetProperty(property.first, property.second))
return false;
}
return true;
}
bool Element::SetProperty(PropertyId id, const Property& property)
{
return meta->style.SetProperty(id, property);
}
void Element::RemoveProperty(const String& name)
{
auto property_id = StyleSheetSpecification::GetPropertyId(name);
if (property_id != PropertyId::Invalid)
meta->style.RemoveProperty(property_id);
else
{
auto shorthand_id = StyleSheetSpecification::GetShorthandId(name);
if (shorthand_id != ShorthandId::Invalid)
{
auto property_id_set = StyleSheetSpecification::GetShorthandUnderlyingProperties(shorthand_id);
for (auto it = property_id_set.begin(); it != property_id_set.end(); ++it)
meta->style.RemoveProperty(*it);
}
}
}
void Element::RemoveProperty(PropertyId id)
{
meta->style.RemoveProperty(id);
}
const Property* Element::GetProperty(const String& name)
{
return meta->style.GetProperty(StyleSheetSpecification::GetPropertyId(name));
}
const Property* Element::GetProperty(PropertyId id)
{
return meta->style.GetProperty(id);
}
const Property* Element::GetLocalProperty(const String& name)
{
return meta->style.GetLocalProperty(StyleSheetSpecification::GetPropertyId(name));
}
const Property* Element::GetLocalProperty(PropertyId id)
{
return meta->style.GetLocalProperty(id);
}
const PropertyMap& Element::GetLocalStyleProperties()
{
return meta->style.GetLocalStyleProperties();
}
float Element::ResolveLength(NumericValue value)
{
float result = 0.f;
if (Any(value.unit & Unit::LENGTH))
result = meta->style.ResolveNumericValue(value, 0.f);
return result;
}
float Element::ResolveNumericValue(NumericValue value, float base_value)
{
float result = 0.f;
if (Any(value.unit & Unit::NUMERIC))
result = meta->style.ResolveNumericValue(value, base_value);
return result;
}
Vector2f Element::GetContainingBlock()
{
Vector2f containing_block(0, 0);
if (offset_parent != nullptr)
{
using namespace Style;
Position position_property = GetPosition();
const Box& parent_box = offset_parent->GetBox();
if (position_property == Position::Static || position_property == Position::Relative)
{
containing_block = parent_box.GetSize();
}
else if (position_property == Position::Absolute || position_property == Position::Fixed)
{
containing_block = parent_box.GetSize(BoxArea::Padding);
}
}
return containing_block;
}
Style::Position Element::GetPosition()
{
return meta->computed_values.position();
}
Style::Float Element::GetFloat()
{
return meta->computed_values.float_();
}
Style::Display Element::GetDisplay()
{
return meta->computed_values.display();
}
float Element::GetLineHeight()
{
return meta->computed_values.line_height().value;
}
const TransformState* Element::GetTransformState() const noexcept
{
return transform_state.get();
}
bool Element::Project(Vector2f& point) const noexcept
{
if (!transform_state || !transform_state->GetTransform())
return true;
// The input point is in window coordinates. Need to find the projection of the point onto the current element plane,
// taking into account the full transform applied to the element.
if (const Matrix4f* inv_transform = transform_state->GetInverseTransform())
{
// Pick two points forming a line segment perpendicular to the window.
Vector4f window_points[2] = {{point.x, point.y, -10, 1}, {point.x, point.y, 10, 1}};
// Project them into the local element space.
window_points[0] = *inv_transform * window_points[0];
window_points[1] = *inv_transform * window_points[1];
Vector3f local_points[2] = {window_points[0].PerspectiveDivide(), window_points[1].PerspectiveDivide()};
// Construct a ray from the two projected points in the local space of the current element.
// Find the intersection with the z=0 plane to produce our destination point.
Vector3f ray = local_points[1] - local_points[0];
// Only continue if we are not close to parallel with the plane.
if (std::fabs(ray.z) > 1.0f)
{
// Solving the line equation p = p0 + t*ray for t, knowing that p.z = 0, produces the following.
float t = -local_points[0].z / ray.z;
Vector3f p = local_points[0] + ray * t;
point = Vector2f(p.x, p.y);
return true;
}
}
// The transformation matrix is either singular, or the ray is parallel to the element's plane.
return false;
}
PropertiesIteratorView Element::IterateLocalProperties() const
{
return PropertiesIteratorView(MakeUnique<PropertiesIterator>(meta->style.Iterate()));
}
void Element::SetPseudoClass(const String& pseudo_class, bool activate)
{
if (meta->style.SetPseudoClass(pseudo_class, activate, false))
{
// Include siblings in case of RCSS presence of sibling combinators '+', '~'.
DirtyDefinition(DirtyNodes::SelfAndSiblings);
OnPseudoClassChange(pseudo_class, activate);
}
}
bool Element::IsPseudoClassSet(const String& pseudo_class) const
{
return meta->style.IsPseudoClassSet(pseudo_class);
}
bool Element::ArePseudoClassesSet(const StringList& pseudo_classes) const
{
for (const String& pseudo_class : pseudo_classes)
{
if (!IsPseudoClassSet(pseudo_class))
return false;
}
return true;
}
StringList Element::GetActivePseudoClasses() const
{
const PseudoClassMap& pseudo_classes = meta->style.GetActivePseudoClasses();
StringList names;
names.reserve(pseudo_classes.size());
for (auto& pseudo_class : pseudo_classes)
{
names.push_back(pseudo_class.first);
}
return names;
}
void Element::OverridePseudoClass(Element* element, const String& pseudo_class, bool activate)
{
RMLUI_ASSERT(element);
element->GetStyle()->SetPseudoClass(pseudo_class, activate, true);
}
Variant* Element::GetAttribute(const String& name)
{
return GetIf(attributes, name);
}
const Variant* Element::GetAttribute(const String& name) const
{
return GetIf(attributes, name);
}
bool Element::HasAttribute(const String& name) const
{
return attributes.find(name) != attributes.end();
}
void Element::RemoveAttribute(const String& name)
{
auto it = attributes.find(name);
if (it != attributes.end())
{
attributes.erase(it);
ElementAttributes changed_attributes;
changed_attributes.emplace(name, Variant());
OnAttributeChange(changed_attributes);
}
}
Element* Element::GetFocusLeafNode()
{
// If there isn't a focus, then we are the leaf.
if (!focus)
{
return this;
}
// Recurse down the tree until we found the leaf focus element
Element* focus_element = focus;
while (focus_element->focus)
focus_element = focus_element->focus;
return focus_element;
}
Context* Element::GetContext() const
{
ElementDocument* document = GetOwnerDocument();
if (document != nullptr)
return document->GetContext();
return nullptr;
}
void Element::SetAttributes(const ElementAttributes& _attributes)
{
attributes.reserve(attributes.size() + _attributes.size());
for (auto& pair : _attributes)
attributes[pair.first] = pair.second;
OnAttributeChange(_attributes);
}
int Element::GetNumAttributes() const
{
return (int)attributes.size();
}
const String& Element::GetTagName() const
{
return tag;
}
const String& Element::GetId() const
{
return id;
}
void Element::SetId(const String& _id)
{
SetAttribute("id", _id);
}
float Element::GetAbsoluteLeft()
{
return GetAbsoluteOffset(BoxArea::Border).x;
}
float Element::GetAbsoluteTop()
{
return GetAbsoluteOffset(BoxArea::Border).y;
}
float Element::GetClientLeft()
{
return GetBox().GetPosition(client_area).x;
}
float Element::GetClientTop()
{
return GetBox().GetPosition(client_area).y;
}
float Element::GetClientWidth()
{
return GetBox().GetSize(client_area).x - meta->scroll.GetScrollbarSize(ElementScroll::VERTICAL);
}
float Element::GetClientHeight()
{
return GetBox().GetSize(client_area).y - meta->scroll.GetScrollbarSize(ElementScroll::HORIZONTAL);
}
Element* Element::GetOffsetParent()
{
return offset_parent;
}
float Element::GetOffsetLeft()
{
return relative_offset_base.x + relative_offset_position.x;
}
float Element::GetOffsetTop()
{
return relative_offset_base.y + relative_offset_position.y;
}
float Element::GetOffsetWidth()
{
return GetBox().GetSize(BoxArea::Border).x;
}
float Element::GetOffsetHeight()
{
return GetBox().GetSize(BoxArea::Border).y;
}
float Element::GetScrollLeft()
{
return scroll_offset.x;
}
void Element::SetScrollLeft(float scroll_left)
{
const float new_offset = Math::Clamp(Math::Round(scroll_left), 0.0f, GetScrollWidth() - GetClientWidth());
if (new_offset != scroll_offset.x)
{
scroll_offset.x = new_offset;
meta->scroll.UpdateScrollbar(ElementScroll::HORIZONTAL);
DirtyAbsoluteOffset();
DispatchEvent(EventId::Scroll, Dictionary());
}
}
float Element::GetScrollTop()
{
return scroll_offset.y;
}
void Element::SetScrollTop(float scroll_top)
{
const float new_offset = Math::Clamp(Math::Round(scroll_top), 0.0f, GetScrollHeight() - GetClientHeight());
if (new_offset != scroll_offset.y)
{
scroll_offset.y = new_offset;
meta->scroll.UpdateScrollbar(ElementScroll::VERTICAL);
DirtyAbsoluteOffset();
DispatchEvent(EventId::Scroll, Dictionary());
}
}
float Element::GetScrollWidth()
{
return Math::Max(scrollable_overflow_rectangle.x, GetClientWidth());
}
float Element::GetScrollHeight()
{
return Math::Max(scrollable_overflow_rectangle.y, GetClientHeight());
}
ElementStyle* Element::GetStyle() const
{
return &meta->style;
}
ElementDocument* Element::GetOwnerDocument() const
{
#ifdef RMLUI_DEBUG
if (parent && !owner_document)
{
// Since we have a parent but no owner_document, then we must be a 'loose' element -- that is, constructed
// outside of a document and not attached to a child of any element in the hierarchy of a document.
// This check ensures that we didn't just forget to set the owner document.
RMLUI_ASSERT(!parent->GetOwnerDocument());
}
#endif
return owner_document;
}
Element* Element::GetParentNode() const
{
return parent;
}
Element* Element::Closest(const String& selectors) const
{
StyleSheetNode root_node;
StyleSheetNodeListRaw leaf_nodes = StyleSheetParser::ConstructNodes(root_node, selectors);
if (leaf_nodes.empty())
{
Log::Message(Log::LT_WARNING, "Query selector '%s' is empty. In element %s", selectors.c_str(), GetAddress().c_str());
return nullptr;
}
Element* parent = GetParentNode();
while (parent)
{
for (const StyleSheetNode* node : leaf_nodes)
{