| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- /*
- * 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 "../Common/TestsShell.h"
- #include <RmlUi/Core/Context.h>
- #include <RmlUi/Core/Element.h>
- #include <RmlUi/Core/ElementDocument.h>
- #include <RmlUi/Core/Event.h>
- #include <doctest.h>
- using namespace Rml;
- static const char* document_handle_rml = R"(
- <rml>
- <head>
- <title>Handle Test</title>
- <link type="text/rcss" href="/assets/rml.rcss"/>
- <style>
- body {
- left: 0;
- top: 0;
- width: 500px;
- height: 500px;
- background-color: #ccc;
- }
- #handle {
- position: absolute;
- top: -10px;
- left: -10px;
- width: 20px;
- height: 20px;
- background-color: #000
- }
- #target {
- position: absolute;
- background: #a33;
- }
- </style>
- </head>
- <body>
- <handle id="handle"></handle>
- <div id="target"></div>
- </body>
- </rml>
- )";
- TEST_CASE("ElementHandle")
- {
- Context* context = TestsShell::GetContext();
- ElementDocument* document = context->LoadDocumentFromMemory(document_handle_rml, "assets/");
- document->Show();
- Element* target = document->GetElementById("target");
- Element* handle = document->GetElementById("handle");
- REQUIRE(target);
- REQUIRE(handle);
- auto dispatch_mouse_event = [](EventId id, Element* dispatch_target, Vector2f mouse_pos) {
- Dictionary drag_start_parameters;
- drag_start_parameters["mouse_x"] = mouse_pos.x;
- drag_start_parameters["mouse_y"] = mouse_pos.y;
- dispatch_target->DispatchEvent(id, drag_start_parameters);
- };
- auto document_set_size = [&](Vector2f new_size) {
- document->SetProperty(PropertyId::Width, Property{new_size.x, Unit::PX});
- document->SetProperty(PropertyId::Height, Property{new_size.y, Unit::PX});
- context->Update();
- };
- SUBCASE("MoveTargetWithTopLeft")
- {
- handle->SetAttribute("move_target", "target");
- target->SetProperty(PropertyId::Top, Property(0, Unit::PX));
- target->SetProperty(PropertyId::Left, Property(0, Unit::PX));
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {10, 10});
- CHECK(target->GetProperty(PropertyId::Top)->Get<float>() == 10);
- CHECK(target->GetProperty(PropertyId::Left)->Get<float>() == 10);
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(10, 10));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- document_set_size({1000, 1000});
- CHECK(target->GetAbsoluteOffset() == Vector2f(10, 10));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("MoveTargetWithTopLeftRightBottom")
- {
- handle->SetAttribute("move_target", "target");
- target->SetProperty(PropertyId::Top, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Left, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Right, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Bottom, Property(50, Unit::PX));
- context->Update();
- REQUIRE(target->GetAbsoluteOffset() == Vector2f(50, 50));
- REQUIRE(target->GetBox().GetSize() == Vector2f(400, 400));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {10, 10});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(60, 60));
- CHECK(target->GetBox().GetSize() == Vector2f(400, 400));
- document_set_size({1000, 1000});
- CHECK(target->GetAbsoluteOffset() == Vector2f(60, 60));
- CHECK(target->GetBox().GetSize() == Vector2f(900, 900));
- }
- SUBCASE("MoveTargetWithWidthHeightBottomRight")
- {
- handle->SetAttribute("move_target", "target");
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Bottom, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Right, Property(50, Unit::PX));
- context->Update();
- REQUIRE(target->GetBox().GetSize() == Vector2f(100, 100));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {10, 10});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(360, 360));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- document_set_size({1000, 1000});
- CHECK(target->GetAbsoluteOffset() == Vector2f(860, 860));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("MoveTargetWithTopLeftRightBottomAndWidthHeight")
- {
- handle->SetAttribute("move_target", "target");
- target->SetProperty(PropertyId::Top, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Left, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Right, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Bottom, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- context->Update();
- REQUIRE(target->GetAbsoluteOffset() == Vector2f(50, 50));
- REQUIRE(target->GetBox().GetSize() == Vector2f(100, 100));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {10, 10});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(60, 60));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- document_set_size({1000, 1000});
- CHECK(target->GetAbsoluteOffset() == Vector2f(60, 60));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("EdgeMarginDefaultConstrainsMoveTarget")
- {
- handle->SetAttribute("move_target", "target");
- target->SetProperty(PropertyId::Top, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Left, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- context->Update();
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {-1000, -1000});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(0, 0));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("EdgeMarginLengthConstrainsMoveTarget")
- {
- handle->SetAttribute("move_target", "target");
- handle->SetAttribute("edge_margin", "10px 10px 10px 20px");
- target->SetProperty(PropertyId::Top, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Left, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- context->Update();
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {-1000, -1000});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(20, 10));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("EdgeMarginPercentageConstrainsMoveTarget")
- {
- handle->SetAttribute("move_target", "target");
- handle->SetAttribute("edge_margin", "-50%");
- target->SetProperty(PropertyId::Top, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Left, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- context->Update();
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {-1000, -1000});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(-50, -50));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("EdgeMarginNoneUnconstrainsMoveTarget")
- {
- handle->SetAttribute("move_target", "target");
- handle->SetAttribute("edge_margin", "none");
- target->SetProperty(PropertyId::Top, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Left, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- context->Update();
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {-1000, -1000});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(-950, -950));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("EdgeMarginLengthConstrainsSizeTarget")
- {
- handle->SetAttribute("size_target", "target");
- handle->SetAttribute("edge_margin", "10px");
- target->SetProperty(PropertyId::Bottom, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Right, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(350, 350));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {500, 500});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(350, 350));
- CHECK(target->GetBox().GetSize() == Vector2f(140, 140));
- }
- SUBCASE("EdgeMarginPercentageConstrainsSizeTarget")
- {
- handle->SetAttribute("size_target", "target");
- handle->SetAttribute("edge_margin", "-50%");
- target->SetProperty(PropertyId::Bottom, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Right, Property(50, Unit::PX));
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(350, 350));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {500, 500});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(350, 350));
- CHECK(target->GetBox().GetSize() == Vector2f(200, 200));
- }
- SUBCASE("AutoMarginMove")
- {
- handle->SetAttribute("move_target", "target");
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- target->SetProperty("margin", "auto");
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(200, 200));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {10, 10});
- CHECK(target->GetProperty(PropertyId::MarginTop)->Get<float>() == 200);
- CHECK(target->GetProperty(PropertyId::MarginRight)->Get<float>() == 200);
- CHECK(target->GetProperty(PropertyId::MarginBottom)->Get<float>() == 200);
- CHECK(target->GetProperty(PropertyId::MarginLeft)->Get<float>() == 200);
- CHECK(target->GetProperty(PropertyId::Top)->Get<float>() == 10);
- CHECK(target->GetProperty(PropertyId::Left)->Get<float>() == 10);
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(210, 210));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- document_set_size({1000, 1000});
- CHECK(target->GetAbsoluteOffset() == Vector2f(210, 210));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("AutoMarginMoveConstraints")
- {
- handle->SetAttribute("move_target", "target");
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- target->SetProperty("margin", "auto");
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(200, 200));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {-1000, -1000});
- CHECK(target->GetProperty(PropertyId::Top)->Get<float>() == -200);
- CHECK(target->GetProperty(PropertyId::Left)->Get<float>() == -200);
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(0, 0));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- }
- SUBCASE("AutoMarginSize")
- {
- handle->SetAttribute("size_target", "target");
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- target->SetProperty("margin", "auto");
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(200, 200));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {10, 10});
- CHECK(target->GetProperty(PropertyId::MarginTop)->Get<float>() == 200);
- CHECK(target->GetProperty(PropertyId::MarginRight)->Get<float>() == 200);
- CHECK(target->GetProperty(PropertyId::MarginBottom)->Get<float>() == 200);
- CHECK(target->GetProperty(PropertyId::MarginLeft)->Get<float>() == 200);
- CHECK(target->GetProperty(PropertyId::Width)->Get<float>() == 110);
- CHECK(target->GetProperty(PropertyId::Height)->Get<float>() == 110);
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(200, 200));
- CHECK(target->GetBox().GetSize() == Vector2f(110, 110));
- }
- SUBCASE("AutoMarginSizeConstraints")
- {
- handle->SetAttribute("size_target", "target");
- target->SetProperty(PropertyId::Width, Property(100, Unit::PX));
- target->SetProperty(PropertyId::Height, Property(100, Unit::PX));
- target->SetProperty("margin", "auto");
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(200, 200));
- CHECK(target->GetBox().GetSize() == Vector2f(100, 100));
- dispatch_mouse_event(EventId::Dragstart, handle, {0, 0});
- dispatch_mouse_event(EventId::Drag, handle, {1000, 1000});
- context->Update();
- CHECK(target->GetAbsoluteOffset() == Vector2f(200, 200));
- CHECK(target->GetBox().GetSize() == Vector2f(300, 300));
- }
- TestsShell::RenderLoop();
- document->Close();
- TestsShell::ShutdownShell();
- }
|