1 // Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
2 //
3 // Permission to use, copy, modify, and distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 //
15 // Aegisub Project http://www.aegisub.org/
16
17 /// @file visual_tool_clip.cpp
18 /// @brief Rectangular clipping visual typesetting tool
19 /// @ingroup visual_ts
20
21 #include "visual_tool_clip.h"
22
23 #include "ass_dialogue.h"
24 #include "include/aegisub/context.h"
25 #include "selection_controller.h"
26
27 #include <libaegisub/format.h>
28
29 #include <wx/colour.h>
30
VisualToolClip(VideoDisplay * parent,agi::Context * context)31 VisualToolClip::VisualToolClip(VideoDisplay *parent, agi::Context *context)
32 : VisualTool<ClipCorner>(parent, context)
33 , cur_1(0, 0)
34 , cur_2(video_res)
35 {
36 ClipCorner *feats[4];
37 for (auto& feat : feats) {
38 feat = new ClipCorner;
39 features.push_back(*feat);
40 }
41
42 // Attach each feature to the two features it shares edges with
43 // Top-left
44 int i = 0;
45 feats[i]->horiz = feats[1];
46 feats[i]->vert = feats[2];
47 i++;
48
49 // Top-right
50 feats[i]->horiz = feats[0];
51 feats[i]->vert = feats[3];
52 i++;
53
54 // Bottom-left
55 feats[i]->horiz = feats[3];
56 feats[i]->vert = feats[0];
57 i++;
58
59 // Bottom-right
60 feats[i]->horiz = feats[2];
61 feats[i]->vert = feats[1];
62 }
63
Draw()64 void VisualToolClip::Draw() {
65 if (!active_line) return;
66
67 DrawAllFeatures();
68
69 // Draw rectangle
70 gl.SetLineColour(colour[3], 1.0f, 2);
71 gl.SetFillColour(colour[3], 0.0f);
72 gl.DrawRectangle(cur_1, cur_2);
73
74 // Draw outside area
75 gl.SetLineColour(colour[3], 0.0f);
76 gl.SetFillColour(*wxBLACK, 0.5f);
77 if (inverse) {
78 gl.DrawRectangle(cur_1, cur_2);
79 }
80 else {
81 Vector2D v_min = video_pos;
82 Vector2D v_max = video_pos + video_res;
83 Vector2D c_min = cur_1.Min(cur_2);
84 Vector2D c_max = cur_1.Max(cur_2);
85 gl.DrawRectangle(v_min, Vector2D(v_max, c_min));
86 gl.DrawRectangle(Vector2D(v_min, c_max), v_max);
87 gl.DrawRectangle(Vector2D(v_min, c_min), Vector2D(c_min, c_max));
88 gl.DrawRectangle(Vector2D(c_max, c_min), Vector2D(v_max, c_max));
89 }
90 }
91
InitializeHold()92 bool VisualToolClip::InitializeHold() {
93 return true;
94 }
95
UpdateHold()96 void VisualToolClip::UpdateHold() {
97 // Limit to video area
98 cur_1 = video_pos.Max((video_pos + video_res).Min(drag_start));
99 cur_2 = video_pos.Max((video_pos + video_res).Min(mouse_pos));
100
101 SetFeaturePositions();
102 CommitHold();
103 }
104
CommitHold()105 void VisualToolClip::CommitHold() {
106 std::string value = agi::format("(%s,%s)", ToScriptCoords(cur_1.Min(cur_2)).Str(), ToScriptCoords(cur_1.Max(cur_2)).Str());
107
108 for (auto line : c->selectionController->GetSelectedSet()) {
109 // This check is technically not correct as it could be outside of an
110 // override block... but that's rather unlikely
111 bool has_iclip = line->Text.get().find("\\iclip") != std::string::npos;
112 SetOverride(line, has_iclip ? "\\iclip" : "\\clip", value);
113 }
114 }
115
UpdateDrag(ClipCorner * feature)116 void VisualToolClip::UpdateDrag(ClipCorner *feature) {
117 // Update features which share an edge with the dragged one
118 feature->horiz->pos = Vector2D(feature->horiz->pos, feature->pos);
119 feature->vert->pos = Vector2D(feature->pos, feature->vert->pos);
120
121 cur_1 = features.front().pos;
122 cur_2 = features.back().pos;
123
124 CommitHold();
125 }
126
SetFeaturePositions()127 void VisualToolClip::SetFeaturePositions() {
128 auto it = features.begin();
129 (it++)->pos = cur_1; // Top-left
130 (it++)->pos = Vector2D(cur_2, cur_1); // Top-right
131 (it++)->pos = Vector2D(cur_1, cur_2); // Bottom-left
132 it->pos = cur_2; // Bottom-right
133 }
134
DoRefresh()135 void VisualToolClip::DoRefresh() {
136 if (active_line) {
137 GetLineClip(active_line, cur_1, cur_2, inverse);
138 cur_1 = FromScriptCoords(cur_1);
139 cur_2 = FromScriptCoords(cur_2);
140 SetFeaturePositions();
141 }
142 }
143