Mostly rewrite the visual tools and related classes

Convert all coordinates within the visual tools to Vector2D, which has
been significantly extended. Eliminates a lot of issues with accumulated
rounding errors and simplifies a lot of code.

Modernize the visual tools' interactions with the rest of Aegisub by
connecting to signals directly rather than routing everything through
the video display and converting the main visual tool mode toolbar to
the command system.

Extract all references to OpenGL from the visual tools and move them to
OpenGLWrapper as a first step towards making it possible to implement an
alternative video renderer. In the process, eliminate all uses of OpenGL
immediate mode.

Fix a bunch of minor issues and general instability.

Originally committed to SVN as r5823.
This commit is contained in:
Thomas Goyne 2011-11-06 17:18:20 +00:00
parent 2e5cbf079e
commit be77dc8307
51 changed files with 2440 additions and 3195 deletions

View File

@ -1919,6 +1919,10 @@
RelativePath="..\..\src\command\video.cpp"
>
</File>
<File
RelativePath="..\..\src\command\vis_tool.cpp"
>
</File>
</Filter>
<Filter
Name="Preferences"

View File

@ -288,6 +288,7 @@
<ClCompile Include="$(SrcDir)command\timecode.cpp" />
<ClCompile Include="$(SrcDir)command\tool.cpp" />
<ClCompile Include="$(SrcDir)command\video.cpp" />
<ClCompile Include="$(SrcDir)command\vis_tool.cpp" />
<ClCompile Include="$(SrcDir)compat.cpp" />
<ClCompile Include="$(SrcDir)dialog_about.cpp" />
<ClCompile Include="$(SrcDir)dialog_attachments.cpp" />

View File

@ -1196,6 +1196,9 @@
<ClCompile Include="$(SrcDir)command\video.cpp">
<Filter>Commands</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)command\vis_tool.cpp">
<Filter>Commands</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)pen.cpp">
<Filter>Utilities\UI utilities</Filter>
</ClCompile>

View File

@ -160,6 +160,7 @@
#include <wx/msgdlg.h>
#include <wx/mstream.h>
#include <wx/notebook.h>
#include <wx/numformatter.h>
#include <wx/panel.h>
#include <wx/power.h>
#include <wx/protocol/http.h>
@ -170,11 +171,11 @@
#include <wx/regex.h>
#include <wx/sashwin.h>
#include <wx/scrolbar.h>
#include <wx/srchctrl.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/slider.h>
#include <wx/spinctrl.h>
#include <wx/srchctrl.h>
#include <wx/stackwalk.h>
#include <wx/statbmp.h>
#include <wx/statbox.h>

View File

@ -121,6 +121,8 @@ protected:
void AdjustScrollbar();
void SetColumnWidths();
bool IsDisplayed(const AssDialogue *line) const;
// Re-implement functions from BaseSelectionController to add batching
void AnnounceActiveLineChanged(AssDialogue *new_line);
void AnnounceSelectedSetChanged(const Selection &lines_added, const Selection &lines_removed);
@ -139,7 +141,6 @@ public:
void EndBatch();
void SetByFrame(bool state);
bool IsDisplayed(const AssDialogue *line) const;
void SelectRow(int row, bool addToSelected = false, bool select=true);
int GetFirstSelRow() const;
wxArrayInt GetSelection() const;

View File

@ -21,7 +21,8 @@ SRC = \
time.cpp \
timecode.cpp \
tool.cpp \
video.cpp
video.cpp \
vis_tool.cpp
SRC += \
icon.cpp \

View File

@ -88,6 +88,7 @@ namespace cmd {
void init_timecode();
void init_tool();
void init_video();
void init_visual_tools();
void init_builtin_commands() {
LOG_D("command/init") << "Populating command map";
@ -104,6 +105,7 @@ namespace cmd {
init_timecode();
init_tool();
init_video();
init_visual_tools();
}
void clear() {

View File

@ -198,6 +198,13 @@ INSERT_ICON("video/opt/autoscroll", toggle_video_autoscroll)
INSERT_ICON("video/play", button_play)
INSERT_ICON("video/play/line", button_playline)
INSERT_ICON("video/stop", button_pause)
INSERT_ICON("video/tool/clip", visual_clip)
INSERT_ICON("video/tool/cross", visual_standard)
INSERT_ICON("video/tool/drag", visual_move)
INSERT_ICON("video/tool/rotate/xy", visual_rotatexy)
INSERT_ICON("video/tool/rotate/z", visual_rotatez)
INSERT_ICON("video/tool/scale", visual_scale)
INSERT_ICON("video/tool/vector_clip", visual_vector_clip)
INSERT_ICON("video/zoom/in", zoom_in_button)
INSERT_ICON("video/zoom/out", zoom_out_button)

View File

@ -238,10 +238,7 @@ struct video_copy_coordinates : public validator_video_loaded {
void operator()(agi::Context *c) {
if (wxTheClipboard->Open()) {
int x, y;
c->videoBox->videoDisplay->GetMousePosition(&x, &y);
c->videoBox->videoDisplay->ToScriptCoords(&x, &y);
wxTheClipboard->SetData(new wxTextDataObject(wxString::Format("%d,%d", x, y)));
wxTheClipboard->SetData(new wxTextDataObject(c->videoBox->videoDisplay->GetMousePosition().Str()));
wxTheClipboard->Close();
}
}

View File

@ -0,0 +1,142 @@
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
//
// $Id$
/// @file vis_tool.cpp
/// @brief Visual typesetting tools commands
/// @ingroup command visual_ui
///
#include "../config.h"
#include "command.h"
#include "../include/aegisub/context.h"
#include "../video_box.h"
#include "../video_context.h"
#include "../video_display.h"
#include "../visual_tool_clip.h"
#include "../visual_tool_cross.h"
#include "../visual_tool_drag.h"
#include "../visual_tool_rotatexy.h"
#include "../visual_tool_rotatez.h"
#include "../visual_tool_scale.h"
#include "../visual_tool_vector_clip.h"
namespace {
using cmd::Command;
/// @defgroup cmd-visual Visual typesetting tools commands
/// @{
struct validator_video_loaded : public Command {
CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) {
return c->videoController->IsLoaded();
}
};
struct visual_mode_cross : public validator_video_loaded {
CMD_NAME("video/tool/cross")
STR_MENU("Standard")
STR_DISP("Standard")
STR_HELP("Standard mode, double click sets position.")
void operator()(agi::Context *c) {
c->videoBox->videoDisplay->SetTool(new VisualToolCross(c->videoBox->videoDisplay, c));
}
};
struct visual_mode_drag : public validator_video_loaded {
CMD_NAME("video/tool/drag")
STR_MENU("Drag")
STR_DISP("Drag")
STR_HELP("Drag subtitles.")
void operator()(agi::Context *c) {
c->videoBox->videoDisplay->SetTool(new VisualToolDrag(c->videoBox->videoDisplay, c));
}
};
struct visual_mode_rotate_z : public validator_video_loaded {
CMD_NAME("video/tool/rotate/z")
STR_MENU("Rotate Z")
STR_DISP("Rotate Z")
STR_HELP("Rotate subtitles on their Z axis.")
void operator()(agi::Context *c) {
c->videoBox->videoDisplay->SetTool(new VisualToolRotateZ(c->videoBox->videoDisplay, c));
}
};
struct visual_mode_rotate_xy : public validator_video_loaded {
CMD_NAME("video/tool/rotate/xy")
STR_MENU("Rotate XY")
STR_DISP("Rotate XY")
STR_HELP("Rotate subtitles on their X and Y axes.")
void operator()(agi::Context *c) {
c->videoBox->videoDisplay->SetTool(new VisualToolRotateXY(c->videoBox->videoDisplay, c));
}
};
struct visual_mode_scale : public validator_video_loaded {
CMD_NAME("video/tool/scale")
STR_MENU("Scale")
STR_DISP("Scale")
STR_HELP("Scale subtitles on X and Y axes.")
void operator()(agi::Context *c) {
c->videoBox->videoDisplay->SetTool(new VisualToolScale(c->videoBox->videoDisplay, c));
}
};
struct visual_mode_clip : public validator_video_loaded {
CMD_NAME("video/tool/clip")
STR_MENU("Clip")
STR_DISP("Clip")
STR_HELP("Clip subtitles to a rectangle.")
void operator()(agi::Context *c) {
c->videoBox->videoDisplay->SetTool(new VisualToolClip(c->videoBox->videoDisplay, c));
}
};
struct visual_mode_vector_clip : public validator_video_loaded {
CMD_NAME("video/tool/vector_clip")
STR_MENU("Vector Clip")
STR_DISP("Vector Clip")
STR_HELP("Clip subtitles to a vectorial arean.")
void operator()(agi::Context *c) {
c->videoBox->videoDisplay->SetTool(new VisualToolVectorClip(c->videoBox->videoDisplay, c));
}
};
}
/// @}
namespace cmd {
void init_visual_tools() {
reg(new visual_mode_cross);
reg(new visual_mode_drag);
reg(new visual_mode_rotate_z);
reg(new visual_mode_rotate_xy);
reg(new visual_mode_scale);
reg(new visual_mode_clip);
reg(new visual_mode_vector_clip);
}
}

View File

@ -52,7 +52,6 @@
#include "utils.h"
#include "validators.h"
#include "video_context.h"
#include "video_display.h"
#include "video_provider_manager.h"
DialogProperties::DialogProperties(agi::Context *c)

View File

@ -52,7 +52,6 @@
#include "selection_controller.h"
#include "subs_edit_ctrl.h"
#include "subs_grid.h"
#include "video_display.h"
// IDs
enum {

View File

@ -55,7 +55,6 @@
#include "utils.h"
#include "validators.h"
#include "video_context.h"
#include "video_display.h"
/// Window IDs
enum {

View File

@ -342,20 +342,30 @@ void OpenGLTextTexture::Insert(OpenGLTextGlyph &glyph) {
/// Draw a glyph at (x,y)
void OpenGLTextGlyph::Draw(int x,int y) const {
// Store matrix and translate
glPushMatrix();
glTranslatef((float)x,(float)y,0.0f);
glBindTexture(GL_TEXTURE_2D, tex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBegin(GL_QUADS);
glTexCoord2f(x1,y1); glVertex2f(0,0); // Top-left
glTexCoord2f(x1,y2); glVertex2f(0,h); // Bottom-left
glTexCoord2f(x2,y2); glVertex2f(w,h); // Bottom-right
glTexCoord2f(x2,y1); glVertex2f(w,0); // Top-right
glEnd();
float tex_coords[] = {
x1, y1,
x1, y2,
x2, y2,
x2, y1
};
glPopMatrix();
float vert_coords[] = {
x, y,
x, y + h,
x + w, y + h,
x + w, y
};
glVertexPointer(2, GL_FLOAT, 0, vert_coords);
glTexCoordPointer(2, GL_FLOAT, 0, tex_coords);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
/// @brief DOCME

View File

@ -1,29 +1,16 @@
// Copyright (c) 2007, Rodrigo Braz Monteiro
// All rights reserved.
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
//
@ -42,303 +29,417 @@
#ifndef AGI_PRE
#include <wx/msgdlg.h>
#ifdef __APPLE__
#ifdef HAVE_APPLE_OPENGL_FRAMEWORK
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include "gl/glext.h"
#endif
#endif
static const float deg2rad = 3.1415926536f / 180.f;
static const float rad2deg = 180.f / 3.1415926536f;
static const float pi = 3.1415926535897932384626433832795f;
#ifdef __WIN32__
#define glGetProc(a) wglGetProcAddress(a)
#elif !defined(__APPLE__)
#include <GL/glx.h>
#define glGetProc(a) glXGetProcAddress((const GLubyte *)(a))
#endif
#if defined(__APPLE__)
// Not required on OS X.
#define APIENTRY
#define GL_EXT(type, name)
#else
#define GL_EXT(type, name) \
static type name = reinterpret_cast<type>(glGetProc(#name)); \
if (!name) { \
name = reinterpret_cast<type>(& name ## Fallback); \
}
#endif
class VertexArray {
std::vector<float> data;
size_t dim;
public:
VertexArray(size_t dims, size_t elems) {
SetSize(dims, elems);
}
void SetSize(size_t dims, size_t elems) {
dim = dims;
data.resize(elems * dim);
}
void Set(size_t i, float x, float y) {
data[i * dim] = x;
data[i * dim + 1] = y;
}
void Set(size_t i, float x, float y, float z) {
data[i * dim] = x;
data[i * dim + 1] = y;
data[i * dim + 2] = z;
}
void Set(size_t i, Vector2D p) {
data[i * dim] = p.X();
data[i * dim + 1] = p.Y();
}
void Draw(GLenum mode, bool clear = true) {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(dim, GL_FLOAT, 0, &data[0]);
glDrawArrays(mode, 0, data.size() / dim);
glDisableClientState(GL_VERTEX_ARRAY);
if (clear)
data.clear();
}
};
/// @brief Constructor
///
OpenGLWrapper::OpenGLWrapper() {
r1 = g1 = b1 = a1 = 1.0f;
r2 = g2 = b2 = a2 = 1.0f;
lw = 1;
line_r = line_g = line_b = line_a = 1.f;
fill_r = fill_g = fill_b = fill_a = 1.f;
line_width = 1;
transform_pushed = false;
smooth = true;
}
/// @brief Draw line
/// @param x1
/// @param y1
/// @param x2
/// @param y2
///
void OpenGLWrapper::DrawLine(float x1,float y1,float x2,float y2) const {
void OpenGLWrapper::DrawLine(Vector2D p1, Vector2D p2) const {
SetModeLine();
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
VertexArray buf(2, 2);
buf.Set(0, p1);
buf.Set(1, p2);
buf.Draw(GL_LINES);
}
static inline Vector2D interp(Vector2D p1, Vector2D p2, float t) {
return t * p1 + (1 - t) * p2;
}
/// @brief Draw line
/// @param x1
/// @param y1
/// @param x2
/// @param y2
/// @param step
///
void OpenGLWrapper::DrawDashedLine(float x1,float y1,float x2,float y2,float step) const {
float dist = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
int steps = (int)((dist-20)/step);
double stepx = double(x2-x1)/steps;
double stepy = double(y2-y1)/steps;
for (int i=0;i<steps;i++) {
if (i % 2 == 0) DrawLine(x1+int(i*stepx),y1+int(i*stepy),x1+int((i+1)*stepx),y1+int((i+1)*stepy));
void OpenGLWrapper::DrawDashedLine(Vector2D p1, Vector2D p2, float step) const {
float dist = (p2 - p1).Len();
step /= dist;
dist -= step;
for (float t = 0; t < 1.f; t += 2 * step) {
DrawLine(interp(p1, p2, t), interp(p1, p2, t + step));
}
}
/// @brief Draw circle
/// @param x
/// @param y
/// @param radiusX
/// @param radiusY
///
void OpenGLWrapper::DrawEllipse(float x,float y,float radiusX,float radiusY) const {
DrawRing(x,y,radiusY,radiusY,radiusX/radiusY);
void OpenGLWrapper::DrawEllipse(Vector2D center, Vector2D radius) const {
DrawRing(center, radius.Y(), radius.Y(), radius.X() / radius.Y());
}
void OpenGLWrapper::DrawRectangle(Vector2D p1, Vector2D p2) const {
VertexArray buf(2, 4);
buf.Set(0, p1);
buf.Set(1, Vector2D(p2, p1));
buf.Set(2, p2);
buf.Set(3, Vector2D(p1, p2));
/// @brief Draw rectangle
/// @param x1
/// @param y1
/// @param x2
/// @param y2
///
void OpenGLWrapper::DrawRectangle(float x1,float y1,float x2,float y2) const {
// Fill
if (a2 != 0.0) {
if (fill_a != 0.0) {
SetModeFill();
glBegin(GL_QUADS);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);
glEnd();
buf.Draw(GL_QUADS, false);
}
// Outline
if (a1 != 0.0) {
if (line_a != 0.0) {
SetModeLine();
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);
glEnd();
buf.Draw(GL_LINE_LOOP);
}
}
void OpenGLWrapper::DrawTriangle(Vector2D p1, Vector2D p2, Vector2D p3) const {
VertexArray buf(2, 3);
buf.Set(0, p1);
buf.Set(1, p2);
buf.Set(2, p3);
/// @brief Draw triangle
/// @param x1
/// @param y1
/// @param x2
/// @param y2
/// @param x3
/// @param y3
///
void OpenGLWrapper::DrawTriangle(float x1,float y1,float x2,float y2,float x3,float y3) const {
// Fill
if (a2 != 0.0) {
if (fill_a != 0.0) {
SetModeFill();
glBegin(GL_TRIANGLES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glEnd();
buf.Draw(GL_TRIANGLES, false);
}
// Outline
if (a1 != 0.0) {
if (line_a != 0.0) {
SetModeLine();
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glEnd();
buf.Draw(GL_LINE_LOOP);
}
}
/// @brief Draw ring (annulus)
/// @param x
/// @param y
/// @param r1
/// @param r2
/// @param ar
/// @param arcStart
/// @param arcEnd
///
void OpenGLWrapper::DrawRing(float x,float y,float r1,float r2,float ar,float arcStart,float arcEnd) const {
// Make r1 bigger
if (r2 > r1) {
float temp = r1;
r1 = r2;
r2 = temp;
}
void OpenGLWrapper::DrawRing(Vector2D center, float r1, float r2, float ar, float arc_start, float arc_end) const {
if (r2 > r1)
std::swap(r1, r2);
// Arc range
bool hasEnds = arcStart != arcEnd;
float pi = 3.1415926535897932384626433832795f;
arcEnd *= pi / 180.f;
arcStart *= pi / 180.f;
if (arcEnd <= arcStart) arcEnd += 2.0f*pi;
float range = arcEnd - arcStart;
bool needs_end_caps = arc_start != arc_end;
arc_end *= deg2rad;
arc_start *= deg2rad;
if (arc_end <= arc_start)
arc_end += 2.f * pi;
float range = arc_end - arc_start;
// Math
int steps = int((r1 + r1*ar) * range / (2.0f*pi))*4;
if (steps < 12) steps = 12;
//float end = arcEnd;
float step = range/steps;
float curAngle = arcStart;
int steps = std::max<int>(((r1 + r1 * ar) * range / (2.f * pi)) * 4, 12);
float step = range / steps;
float cur_angle = arc_start;
// Fill
if (a2 != 0.0) {
VertexArray buf(2, steps);
Vector2D scale_inner = Vector2D(ar, 1) * r1;
Vector2D scale_outer = Vector2D(ar, 1) * r2;
if (fill_a != 0.0) {
SetModeFill();
// Annulus
if (r1 != r2) {
glBegin(GL_QUADS);
for (int i=0;i<steps;i++) {
glVertex2f(x+sin(curAngle)*r1*ar,y+cos(curAngle)*r1);
glVertex2f(x+sin(curAngle)*r2*ar,y+cos(curAngle)*r2);
curAngle += step;
glVertex2f(x+sin(curAngle)*r2*ar,y+cos(curAngle)*r2);
glVertex2f(x+sin(curAngle)*r1*ar,y+cos(curAngle)*r1);
buf.SetSize(2, (steps + 1) * 2);
for (int i = 0; i <= steps; i++) {
Vector2D offset = Vector2D::FromAngle(cur_angle);
buf.Set(i * 2 + 0, center + offset * scale_inner);
buf.Set(i * 2 + 1, center + offset * scale_outer);
cur_angle += step;
}
glEnd();
buf.Draw(GL_QUAD_STRIP);
}
// Circle
else {
glBegin(GL_POLYGON);
for (int i=0;i<steps;i++) {
glVertex2f(x+sin(curAngle)*r1,y+cos(curAngle)*r1);
curAngle += step;
buf.SetSize(2, steps);
for (int i = 0; i < steps; i++) {
buf.Set(i, center + Vector2D::FromAngle(cur_angle) * scale_inner);
cur_angle += step;
}
glEnd();
buf.Draw(GL_POLYGON);
}
// Reset angle
curAngle = arcStart;
cur_angle = arc_start;
}
// Outlines
if (a1 != 0.0) {
// Outer
steps++;
SetModeLine();
glBegin(GL_LINE_STRIP);
for (int i=0;i<steps;i++) {
glVertex2f(x+sin(curAngle)*r1,y+cos(curAngle)*r1);
curAngle += step;
}
glEnd();
if (line_a == 0.0) return;
// Inner
if (r1 != r2) {
curAngle = arcStart;
glBegin(GL_LINE_STRIP);
for (int i=0;i<steps;i++) {
glVertex2f(x+sin(curAngle)*r2,y+cos(curAngle)*r2);
curAngle += step;
}
glEnd();
// Outer
steps++;
buf.SetSize(2, steps);
// End caps
if (hasEnds) {
glBegin(GL_LINES);
glVertex2f(x+sin(arcStart)*r1,y+cos(arcStart)*r1);
glVertex2f(x+sin(arcStart)*r2,y+cos(arcStart)*r2);
glVertex2f(x+sin(arcEnd)*r1,y+cos(arcEnd)*r1);
glVertex2f(x+sin(arcEnd)*r2,y+cos(arcEnd)*r2);
glEnd();
}
}
SetModeLine();
for (int i = 0; i < steps; i++) {
buf.Set(i, center + Vector2D::FromAngle(cur_angle) * scale_outer);
cur_angle += step;
}
buf.Draw(GL_LINE_STRIP);
// Inner
if (r1 == r2) return;
cur_angle = arc_start;
buf.SetSize(2, steps);
for (int i = 0; i < steps; i++) {
buf.Set(i, center + Vector2D::FromAngle(cur_angle) * scale_inner);
cur_angle += step;
}
buf.Draw(GL_LINE_STRIP);
if (!needs_end_caps) return;
buf.SetSize(2, 4);
buf.Set(0, center + Vector2D::FromAngle(arc_start) * scale_inner);
buf.Set(1, center + Vector2D::FromAngle(arc_start) * scale_outer);
buf.Set(2, center + Vector2D::FromAngle(arc_end) * scale_inner);
buf.Set(3, center + Vector2D::FromAngle(arc_end) * scale_outer);
buf.Draw(GL_LINES);
}
/// @brief Set line colour
/// @param col
/// @param alpha
/// @param width
///
void OpenGLWrapper::SetLineColour(wxColour col,float alpha,int width) {
r1 = col.Red()/255.0f;
g1 = col.Green()/255.0f;
b1 = col.Blue()/255.0f;
a1 = alpha;
lw = width;
void OpenGLWrapper::SetLineColour(wxColour col, float alpha, int width) {
line_r = col.Red() / 255.f;
line_g = col.Green() / 255.f;
line_b = col.Blue() / 255.f;
line_a = alpha;
line_width = width;
}
/// @brief Set fill colour
/// @param col
/// @param alpha
///
void OpenGLWrapper::SetFillColour(wxColour col,float alpha) {
r2 = col.Red()/255.0f;
g2 = col.Green()/255.0f;
b2 = col.Blue()/255.0f;
a2 = alpha;
void OpenGLWrapper::SetFillColour(wxColour col, float alpha) {
fill_r = col.Red() / 255.f;
fill_g = col.Green() / 255.f;
fill_b = col.Blue() / 255.f;
fill_a = alpha;
}
/// @brief Line
///
void OpenGLWrapper::SetModeLine() const {
glColor4f(r1,g1,b1,a1);
glColor4f(line_r, line_g, line_b, line_a);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(lw);
glEnable(GL_LINE_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(line_width);
if (smooth)
glEnable(GL_LINE_SMOOTH);
else
glDisable(GL_LINE_SMOOTH);
}
/// @brief Fill
///
void OpenGLWrapper::SetModeFill() const {
glColor4f(r2,g2,b2,a2);
if (a2 == 1.0f) glDisable(GL_BLEND);
glColor4f(fill_r, fill_g, fill_b, fill_a);
if (fill_a == 1.f) glDisable(GL_BLEND);
else {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
}
void OpenGLWrapper::SetInvert() {
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_INVERT);
/// @brief Is extension supported?
/// @param ext
/// @return
///
bool OpenGLWrapper::IsExtensionSupported(const char *ext) {
char *extList = (char*) glGetString(GL_EXTENSIONS);
if (!extList) return false;
return strstr(extList, ext) != NULL;
// GL_LINE_SMOOTH combines badly with inverting
smooth = false;
}
void OpenGLWrapper::ClearInvert() {
glDisable(GL_COLOR_LOGIC_OP);
smooth = true;
}
bool OpenGLWrapper::IsExtensionSupported(const char *ext) {
char *extList = (char * )glGetString(GL_EXTENSIONS);
return extList && !!strstr(extList, ext);
}
void OpenGLWrapper::DrawLines(size_t dim, std::vector<float> const& lines) {
DrawLines(dim, &lines[0], lines.size() / dim);
}
/// DOCME
wxMutex OpenGLWrapper::glMutex;
void OpenGLWrapper::DrawLines(size_t dim, std::vector<float> const& lines, size_t c_dim, std::vector<float> const& colors) {
glShadeModel(GL_SMOOTH);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(c_dim, GL_FLOAT, 0, &colors[0]);
DrawLines(dim, &lines[0], lines.size() / dim);
glDisableClientState(GL_COLOR_ARRAY);
glShadeModel(GL_FLAT);
}
void OpenGLWrapper::DrawLines(size_t dim, const float *lines, size_t n) {
SetModeLine();
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(dim, GL_FLOAT, 0, lines);
glDrawArrays(GL_LINES, 0, n);
glDisableClientState(GL_VERTEX_ARRAY);
}
void OpenGLWrapper::DrawLineStrip(size_t dim, std::vector<float> const& lines) {
SetModeLine();
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(dim, GL_FLOAT, 0, &lines[0]);
glDrawArrays(GL_LINE_STRIP, 0, lines.size() / dim);
glDisableClientState(GL_VERTEX_ARRAY);
}
// Substitute for glMultiDrawArrays for sub-1.4 OpenGL
// Not required on OS X.
#ifndef __APPLE__
static void APIENTRY glMultiDrawArraysFallback(GLenum mode, const GLint *first, const GLsizei *count, GLsizei primcount) {
for (int i = 0; i < primcount; ++i) {
glDrawArrays(mode, *first++, *count++);
}
}
#endif
void OpenGLWrapper::DrawMultiPolygon(std::vector<float> const& points, std::vector<int> &start, std::vector<int> &count, Vector2D video_size, bool invert) {
GL_EXT(PFNGLMULTIDRAWARRAYSPROC, glMultiDrawArrays);
// The following is nonzero winding-number PIP based on stencils
// Draw to stencil only
glEnable(GL_STENCIL_TEST);
glColorMask(0, 0, 0, 0);
// GL_INCR_WRAP was added in 1.4, so instead set the entire stencil to 128
// and wobble from there
glStencilFunc(GL_NEVER, 128, 0xFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
VertexArray buf(2, 4);
buf.Set(0, Vector2D());
buf.Set(1, Vector2D(video_size, 0));
buf.Set(2, video_size);
buf.Set(3, Vector2D(0, video_size));
glColor4f(0, 0, 0, 1);
glDisable(GL_BLEND);
buf.Draw(GL_QUADS, false);
// Increment the winding number for each forward facing triangle
glStencilOp(GL_INCR, GL_INCR, GL_INCR);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, &points[0]);
glMultiDrawArrays(GL_TRIANGLE_FAN, &start[0], &count[0], start.size());
// Decrement the winding number for each backfacing triangle
glStencilOp(GL_DECR, GL_DECR, GL_DECR);
glCullFace(GL_FRONT);
glMultiDrawArrays(GL_TRIANGLE_FAN, &start[0], &count[0], start.size());
glDisable(GL_CULL_FACE);
// Draw the actual rectangle
glColorMask(1, 1, 1, 1);
float real_line_a = line_a;
line_a = 0;
// VSFilter draws when the winding number is nonzero, so we want to draw the
// mask when the winding number is zero (where 128 is zero due to the lack of
// wrapping combined with unsigned numbers)
glStencilFunc(invert ? GL_EQUAL : GL_NOTEQUAL, 128, 0xFF);
DrawRectangle(Vector2D(), video_size);
glDisable(GL_STENCIL_TEST);
// Draw lines
line_a = real_line_a;
SetModeLine();
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, &points[0]);
glMultiDrawArrays(GL_LINE_LOOP, &start[0], &count[0], start.size());
glDisableClientState(GL_VERTEX_ARRAY);
}
void OpenGLWrapper::SetOrigin(Vector2D origin) {
PrepareTransform();
glTranslatef(origin.X(), origin.Y(), -1.f);
}
void OpenGLWrapper::SetScale(Vector2D scale) {
PrepareTransform();
glScalef(scale.X() / 100.f, scale.Y() / 100.f, 1.f);
}
void OpenGLWrapper::SetRotation(float x, float y, float z) {
PrepareTransform();
float matrix[16] = { 2500, 0, 0, 0, 0, 2500, 0, 0, 0, 0, 1, 1, 0, 0, 2500, 2500 };
glMultMatrixf(matrix);
glScalef(1.f, 1.f, 8.f);
glRotatef(y, 0.f, -1.f, 0.f);
glRotatef(x, -1.f, 0.f, 0.f);
glRotatef(z, 0.f, 0.f, -1.f);
}
void OpenGLWrapper::PrepareTransform() {
if (!transform_pushed) {
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
transform_pushed = true;
}
}
void OpenGLWrapper::ResetTransform() {
if (transform_pushed) {
glPopMatrix();
transform_pushed = false;
}
}

View File

@ -1,29 +1,16 @@
// Copyright (c) 2007, Rodrigo Braz Monteiro
// All rights reserved.
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
//
@ -34,41 +21,13 @@
/// @ingroup video_output
///
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
/// DOCME
typedef GLuint GLhandleARB;
#endif
#include "vector2d.h"
#ifndef AGI_PRE
#include <wx/thread.h>
#include <wx/colour.h>
#endif
#ifdef __WIN32__
#define glGetProc(a) wglGetProcAddress(a)
#else
#define glGetProc(a) glXGetProcAddress((const GLubyte *)(a))
#endif
#if defined(__APPLE__)
// Not required on OS X.
#define GL_EXT(type, name)
#else
#define GL_EXT(type, name) \
static type name = reinterpret_cast<type>(glGetProc(#name)); \
if (!name) { \
name = reinterpret_cast<type>(& name ## Fallback); \
}
#include <vector>
#endif
class wxColour;
/// DOCME
/// @class OpenGLWrapper
@ -76,55 +35,51 @@ typedef GLuint GLhandleARB;
///
/// DOCME
class OpenGLWrapper {
private:
float line_r, line_g, line_b, line_a;
float fill_r, fill_g, fill_b, fill_a;
/// DOCME
int line_width;
bool smooth;
/// DOCME
/// DOCME
/// DOCME
float r1,g1,b1,a1;
/// DOCME
/// DOCME
/// DOCME
/// DOCME
float r2,g2,b2,a2;
/// DOCME
int lw;
bool transform_pushed;
void PrepareTransform();
public:
OpenGLWrapper();
/// DOCME
static wxMutex glMutex;
void SetLineColour(wxColour col,float alpha=1.0f,int width=1);
void SetFillColour(wxColour col,float alpha=1.0f);
void SetLineColour(wxColour col, float alpha = 1.0f, int width = 1);
void SetFillColour(wxColour col, float alpha = 1.0f);
void SetModeLine() const;
void SetModeFill() const;
void DrawLine(float x1,float y1,float x2,float y2) const;
void DrawDashedLine(float x1,float y1,float x2,float y2,float dashLen) const;
void DrawEllipse(float x,float y,float radiusX,float radiusY) const;
/// @brief DOCME
/// @param x
/// @param y
/// @param radius
///
void DrawCircle(float x,float y,float radius) const { DrawEllipse(x,y,radius,radius); }
void DrawRectangle(float x1,float y1,float x2,float y2) const;
void DrawRing(float x,float y,float r1,float r2,float ar=1.0f,float arcStart=0.0f,float arcEnd=0.0f) const;
void DrawTriangle(float x1,float y1,float x2,float y2,float x3,float y3) const;
void SetInvert();
void ClearInvert();
void SetScale(Vector2D scale);
void SetOrigin(Vector2D origin);
void SetRotation(float x, float y, float z);
void ResetTransform();
void DrawLine(Vector2D p1, Vector2D p2) const;
void DrawDashedLine(Vector2D p1, Vector2D p2, float dashLen) const;
void DrawEllipse(Vector2D center, Vector2D radius) const;
void DrawCircle(Vector2D center, float radius) const { DrawEllipse(center, Vector2D(radius, radius)); }
void DrawRectangle(Vector2D p1, Vector2D p2) const;
void DrawRing(Vector2D center, float r1, float r2, float ar = 1.0f, float arcStart = 0.0f, float arcEnd = 0.0f) const;
void DrawTriangle(Vector2D p1, Vector2D p2, Vector2D p3) const;
void DrawLines(size_t dim, std::vector<float> const& lines);
void DrawLines(size_t dim, std::vector<float> const& lines, size_t c_dim, std::vector<float> const& colors);
void DrawLines(size_t dim, const float *lines, size_t n);
void DrawLineStrip(size_t dim, std::vector<float> const& lines);
/// Draw a multipolygon serialized into a single array
/// @param points List of coordinates
/// @param start Indices in points which are the start of a new polygon
/// @param count Number of points in each polygon
/// @param video_size Bottom-right corner of the visible area
/// @param invert Draw the area outside the polygons instead
void DrawMultiPolygon(std::vector<float> const& points, std::vector<int> &start, std::vector<int> &count, Vector2D video_size, bool invert);
static bool IsExtensionSupported(const char *ext);
};

View File

@ -34,5 +34,5 @@ namespace toolbar {
/// @param context Project context
/// @param hotkey Hotkey context for the tooltip
void AttachToolbar(wxFrame *frame, std::string const& name, agi::Context *context, std::string const& hotkey);
wxToolBar *GetToolbar(wxWindow *parent, std::string const& name, agi::Context *context, std::string const& hotkey);
wxToolBar *GetToolbar(wxWindow *parent, std::string const& name, agi::Context *context, std::string const& hotkey, bool vertical = false);
}

View File

@ -474,43 +474,43 @@
"key" : "Left"
}
],
"visual typesetting set tool crosshair" : [
"video/tool/cross" : [
{
"modifiers" : [],
"key" : "A"
}
],
"visual typesetting set tool drag" : [
"video/tool/drag" : [
{
"modifiers" : [],
"key" : "S"
}
],
"visual typesetting set tool rectangle clip" : [
"video/tool/clip" : [
{
"modifiers" : [],
"key" : "H"
}
],
"visual typesetting set tool rotate xy" : [
"video/tool/rotate/xy" : [
{
"modifiers" : [],
"key" : "F"
}
],
"visual typesetting set tool rotate z" : [
"video/tool/rotate/z" : [
{
"modifiers" : [],
"key" : "D"
}
],
"visual typesetting set tool scale" : [
"video/tool/scale" : [
{
"modifiers" : [],
"key" : "G"
}
],
"visual typesetting set tool vector clip" : [
"video/tool/vector_clip" : [
{
"modifiers" : [],
"key" : "J"

View File

@ -61,5 +61,16 @@
"",
"app/options",
"grid/tag/cycle_hiding"
],
"visual_tools" : [
"video/tool/cross",
"video/tool/drag",
"video/tool/rotate/z",
"video/tool/rotate/xy",
"video/tool/scale",
"video/tool/clip",
"video/tool/vector_clip",
"",
"help/video"
]
}

View File

@ -37,192 +37,164 @@
#include "config.h"
#ifndef AGI_PRE
#include <limits>
#include <wx/tokenzr.h>
#endif
#include <limits>
#include "spline.h"
#include "utils.h"
#include "video_display.h"
/// @brief Spline constructor
Spline::Spline(const VideoDisplay &scale) : scale(scale) {
#include "utils.h"
#include "visual_tool.h"
Spline::Spline(const VisualToolBase &scale) : scale(scale) {
}
/// @brief Encode to ASS
wxString Spline::EncodeToASS() {
wxString result;
char lastCommand = 0;
result.reserve(size() * 10);
char last = 0;
// Insert each element
for (iterator cur=begin();cur!=end();cur++) {
// Each curve
for (iterator cur = begin(); cur != end(); ++cur) {
switch (cur->type) {
case CURVE_POINT: {
if (lastCommand != 'm') {
case SplineCurve::POINT:
if (last != 'm') {
result += "m ";
lastCommand = 'm';
last = 'm';
}
int x = cur->p1.x;
int y = cur->p1.y;
scale.ToScriptCoords(&x, &y);
result += wxString::Format("%i %i ", x, y);
result += scale.ToScriptCoords(cur->p1).DStr(' ');
break;
}
case CURVE_LINE: {
if (lastCommand != 'l') {
case SplineCurve::LINE:
if (last != 'l') {
result += "l ";
lastCommand = 'l';
last = 'l';
}
int x = cur->p2.x;
int y = cur->p2.y;
scale.ToScriptCoords(&x, &y);
result += wxString::Format("%i %i ", x, y);
result += scale.ToScriptCoords(cur->p2).DStr(' ');
break;
}
case CURVE_BICUBIC: {
if (lastCommand != 'b') {
case SplineCurve::BICUBIC:
if (last != 'b') {
result += "b ";
lastCommand = 'b';
last = 'b';
}
int x2 = cur->p2.x;
int y2 = cur->p2.y;
int x3 = cur->p3.x;
int y3 = cur->p3.y;
int x4 = cur->p4.x;
int y4 = cur->p4.y;
scale.ToScriptCoords(&x2, &y2);
scale.ToScriptCoords(&x3, &y3);
scale.ToScriptCoords(&x4, &y4);
result += wxString::Format("%i %i %i %i %i %i ", x2, y2, x3, y3, x4, y4);
result += scale.ToScriptCoords(cur->p2).DStr(' ');
result += scale.ToScriptCoords(cur->p3).DStr(' ');
result += scale.ToScriptCoords(cur->p4).DStr(' ');
break;
}
default: break;
}
result += " ";
}
return result;
}