cool-retro-term/app/Terminal.qml

376 lines
13 KiB
QML
Raw Normal View History

/*******************************************************************************
* Copyright (c) 2013 "Filippo Scognamiglio"
* https://github.com/Swordifish90/cool-old-term
*
* This file is part of cool-old-term.
*
* cool-old-term is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
import QtQuick 2.2
2014-03-23 18:29:19 +01:00
import QtGraphicalEffects 1.0
import QtQuick.Controls 1.1
import org.kde.konsole 0.1
Item{
id: terminalContainer
property variant theSource: finalSource
property variant bloomSource: bloomSourceLoader.item
property variant rasterizationSource: rasterizationEffectSource
2014-06-11 00:54:47 +02:00
property variant staticNoiseSource: staticNoiseSource
2014-06-07 11:33:37 +02:00
property alias kterminal: kterminal
signal sizeChanged
onWidthChanged: sizeChanged()
onHeightChanged: sizeChanged()
//The blur effect has to take into account the framerate
property real fpsAttenuation: 60 / shadersettings.fps
2014-03-23 18:29:19 +01:00
property real mBlur: shadersettings.motion_blur
property real motionBlurCoefficient: (_maxBlurCoefficient * mBlur + _minBlurCoefficient * (1 - mBlur))
property real _minBlurCoefficient: 0.75
property real _maxBlurCoefficient: 0.95
2014-03-23 18:29:19 +01:00
property real scanlineWidth: 1
property real scanlineHeight: 1
property size virtual_resolution: Qt.size(width / scanlineWidth, height / scanlineHeight)
property real deltay: 0.5 / virtual_resolution.height
property real deltax: 0.5 / virtual_resolution.width
property real mBloom: shadersettings.bloom_strength
property int mScanlines: shadersettings.rasterization
onMScanlinesChanged: restartBlurredSource()
2014-06-07 02:19:37 +02:00
property size terminalSize: kterminal.terminalSize
property size paintedTextSize
onPaintedTextSizeChanged: console.log(paintedTextSize)
2014-04-17 13:27:41 +02:00
//Force reload of the blursource when settings change
onMBlurChanged: restartBlurredSource()
function restartBlurredSource(){
if(!blurredSource) return;
blurredSource.live = true;
livetimer.restart()
}
2014-04-16 19:30:11 +02:00
function pasteClipboard(){
2014-06-07 02:19:37 +02:00
kterminal.pasteClipboard();
2014-04-16 19:30:11 +02:00
}
function copyClipboard(){
2014-06-07 02:19:37 +02:00
kterminal.copyClipboard();
2014-04-16 19:30:11 +02:00
}
2014-03-24 23:17:02 +01:00
2014-06-07 02:19:37 +02:00
KTerminal {
id: kterminal
2014-03-30 22:29:15 +02:00
anchors.fill: parent
2014-06-07 02:19:37 +02:00
font.pixelSize: shadersettings.font.pixelSize
font.family: shadersettings.font.name
2014-06-07 02:19:37 +02:00
colorScheme: "MyWhiteOnBlack"
2014-06-07 02:19:37 +02:00
session: KSession {
id: ksession
kbScheme: "linux"
2014-04-17 13:27:41 +02:00
2014-06-07 02:19:37 +02:00
onFinished: {
Qt.quit()
}
2014-06-07 02:19:37 +02:00
}
2014-06-07 02:19:37 +02:00
Text{id: fontMetrics; text: "B"; visible: false}
2014-06-07 02:19:37 +02:00
function handleFontChange(){
var scaling_factor = shadersettings.window_scaling;
var font_size = shadersettings.font.pixelSize * scaling_factor;
font.pixelSize = font_size;
font.family = shadersettings.font.name;
2014-06-07 02:19:37 +02:00
fontMetrics.font = font;
2014-06-07 02:19:37 +02:00
var vertical_density = shadersettings.font.virtualResolution.height;
var horizontal_density = shadersettings.font.virtualResolution.width;
2014-06-07 02:19:37 +02:00
var scanline_height = fontMetrics.paintedHeight / vertical_density;
var scanline_width = fontMetrics.paintedWidth / horizontal_density;
2014-06-07 02:19:37 +02:00
var scanline_spacing = shadersettings.font.lineSpacing;
var line_spacing = Math.round(scanline_spacing * scanline_height);
// console.log("Font height: " + fontMetrics.paintedHeight)
// console.log("Scanline Height: " + scanline_height)
// console.log("Line Spacing: " + line_spacing)
2014-06-07 02:19:37 +02:00
terminalContainer.scanlineHeight = scanline_height;
terminalContainer.scanlineWidth = scanline_width;
2014-06-07 02:19:37 +02:00
setLineSpacing(line_spacing);
restartBlurredSource();
}
Component.onCompleted: {
shadersettings.terminalFontChanged.connect(handleFontChange);
handleFontChange();
forceActiveFocus();
}
}
2014-04-16 19:18:14 +02:00
Menu{
id: contextmenu
2014-04-16 19:30:11 +02:00
MenuItem{action: copyAction}
MenuItem{action: pasteAction}
MenuSeparator{}
MenuItem{action: fullscreenAction}
2014-04-16 19:18:14 +02:00
}
2014-03-30 22:29:15 +02:00
MouseArea{
2014-04-16 19:18:14 +02:00
acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.RightButton
2014-03-30 22:29:15 +02:00
anchors.fill: parent
onWheel:
2014-06-07 02:19:37 +02:00
wheel.angleDelta.y > 0 ? kterminal.scrollUp() : kterminal.scrollDown()
2014-04-16 19:18:14 +02:00
onClicked: {
if (mouse.button == Qt.RightButton){
contextmenu.popup();
} else if (mouse.button == Qt.MiddleButton){
2014-06-07 02:19:37 +02:00
kterminal.pasteSelection();
2014-04-16 19:18:14 +02:00
}
}
onDoubleClicked: {
if (mouse.button == Qt.LeftButton){
var coord = correctDistortion(mouse.x, mouse.y);
2014-06-07 02:19:37 +02:00
kterminal.mouseDoubleClick(coord.width, coord.height);
2014-04-16 19:18:14 +02:00
}
}
onPositionChanged: {
var coord = correctDistortion(mouse.x, mouse.y);
2014-06-07 02:19:37 +02:00
kterminal.mouseMove(coord.width, coord.height);
2014-04-16 19:18:14 +02:00
}
onPressed: {
if (mouse.button == Qt.LeftButton){
var coord = correctDistortion(mouse.x, mouse.y);
2014-06-07 02:19:37 +02:00
kterminal.mousePress(coord.width, coord.height);
2014-04-16 19:18:14 +02:00
}
}
onReleased: {
if (mouse.button == Qt.LeftButton){
2014-06-07 02:19:37 +02:00
kterminal.mouseRelease(mouse.x, mouse.y);
2014-04-16 19:18:14 +02:00
}
}
2014-06-07 11:33:37 +02:00
//Frame displacement properties
property real dtop: frame.item.displacementTop
property real dleft:frame.item.displacementLeft
property real dright: frame.item.displacementRight
property real dbottom: frame.item.displacementBottom
function correctDistortion(x, y){
2014-04-16 19:18:14 +02:00
x = x / width;
y = y / height;
2014-06-07 11:33:37 +02:00
x = (-dleft + x * (width + dleft + dright)) / width
y = (-dtop + y * (height + dtop + dbottom)) / height
2014-04-16 19:18:14 +02:00
var cc = Qt.size(0.5 - x, 0.5 - y);
var distortion = (cc.height * cc.height + cc.width * cc.width) * shadersettings.screen_distortion;
return Qt.size((x - cc.width * (1+distortion) * distortion) * width,
(y - cc.height * (1+distortion) * distortion) * height)
}
2014-03-30 22:29:15 +02:00
}
2014-03-23 18:29:19 +01:00
ShaderEffectSource{
id: source
sourceItem: kterminal
hideSource: true
smooth: false
2014-03-23 18:29:19 +01:00
}
ShaderEffectSource{
id: blurredSource
sourceItem: blurredterminal
recursive: true
live: true
smooth: false
antialiasing: false
Timer{
id: livetimer
running: true
onTriggered: parent.live = false;
2014-06-07 02:19:37 +02:00
function updateImageHandler(){
livetimer.restart();
blurredSource.live = true;
}
Component.onCompleted: kterminal.updatedImage.connect(updateImageHandler);
2014-03-23 18:29:19 +01:00
}
}
ShaderEffectSource{
id: finalSource
sourceItem: blurredterminal
sourceRect: frame.sourceRect
format: ShaderEffectSource.Alpha
}
2014-03-23 18:29:19 +01:00
ShaderEffect {
id: blurredterminal
anchors.fill: parent
property variant source: source
property variant blurredSource: (mBlur !== 0) ? blurredSource : undefined
property size virtual_resolution: parent.virtual_resolution
property size delta: Qt.size((mScanlines == shadersettings.pixel_rasterization ? deltax : 0),
2014-06-07 02:19:37 +02:00
mScanlines != shadersettings.no_rasterization ? deltay : 0)
2014-06-11 00:54:47 +02:00
blending: false
2014-03-23 18:29:19 +01:00
fragmentShader:
"uniform lowp float qt_Opacity;" +
"uniform lowp sampler2D source;" +
"uniform highp vec2 delta;" +
"varying highp vec2 qt_TexCoord0;
2014-03-23 18:29:19 +01:00
uniform highp vec2 virtual_resolution;" +
2014-03-23 18:29:19 +01:00
2014-06-07 02:19:37 +02:00
(mBlur !== 0 ?
"uniform lowp sampler2D blurredSource;"
: "") +
2014-03-23 18:29:19 +01:00
2014-06-07 02:19:37 +02:00
"void main() {" +
"vec2 coords = qt_TexCoord0;" +
(mScanlines != shadersettings.no_rasterization ? "
coords.y = floor(virtual_resolution.y * coords.y) / virtual_resolution.y;" +
2014-06-07 02:19:37 +02:00
(mScanlines == shadersettings.pixel_rasterization ? "
coords.x = floor(virtual_resolution.x * coords.x) / virtual_resolution.x;" : "")
2014-06-07 02:19:37 +02:00
: "") +
2014-06-20 01:54:14 +02:00
"coords = coords + delta;" +
"float color = texture2D(source, coords).r * 256.0;" +
2014-06-07 02:19:37 +02:00
(mBlur !== 0 ?
2014-06-20 01:54:14 +02:00
"float blurredSourceColor = texture2D(blurredSource, coords).a * 256.0;" +
2014-06-07 02:19:37 +02:00
"blurredSourceColor = blurredSourceColor - blurredSourceColor * " + (1.0 - motionBlurCoefficient) * fpsAttenuation+ ";" +
"color = step(1.0, color) * color + step(color, 1.0) * blurredSourceColor;"
: "") +
2014-06-20 01:54:14 +02:00
"gl_FragColor.a = floor(color) / 256.0;" +
2014-06-07 02:19:37 +02:00
"}"
2014-03-23 18:29:19 +01:00
}
//////////////////////////////////////////////////////////////////////
//EFFECTS
//////////////////////////////////////////////////////////////////////
//Bloom
Loader{
id: bloomEffectLoader
active: mBloom != 0
anchors.fill: parent
sourceComponent: FastBlur{
radius: 32
source: kterminal
transparentBorder: true
smooth: false
}
}
Loader{
id: bloomSourceLoader
active: mBloom != 0
sourceComponent: ShaderEffectSource{
sourceItem: bloomEffectLoader.item
hideSource: true
sourceRect: frame.sourceRect
smooth: false
}
}
2014-06-11 00:54:47 +02:00
//Rasterization mask
ShaderEffect {
id: staticNoiseEffect
anchors.fill: parent
property size virtual_resolution: terminalContainer.virtual_resolution
blending: false
fragmentShader:
"uniform lowp float qt_Opacity;
varying highp vec2 qt_TexCoord0;
uniform highp vec2 virtual_resolution;" +
2014-06-20 01:54:14 +02:00
"highp float noise(vec2 co)
2014-06-11 00:54:47 +02:00
{
highp float a = 12.9898;
highp float b = 78.233;
highp float c = 43758.5453;
highp float dt= dot(co.xy ,vec2(a,b));
highp float sn= mod(dt,3.14);
return fract(sin(sn) * c);
}
2014-06-20 01:54:14 +02:00
vec2 sw(vec2 p) {return vec2( floor(p.x) , floor(p.y) );}
vec2 se(vec2 p) {return vec2( ceil(p.x) , floor(p.y) );}
vec2 nw(vec2 p) {return vec2( floor(p.x) , ceil(p.y) );}
vec2 ne(vec2 p) {return vec2( ceil(p.x) , ceil(p.y) );}
float smoothNoise(vec2 p) {
vec2 inter = smoothstep(0., 1., fract(p));
float s = mix(noise(sw(p)), noise(se(p)), inter.x);
float n = mix(noise(nw(p)), noise(ne(p)), inter.x);
return mix(s, n, inter.y);
2014-06-11 00:54:47 +02:00
}" +
"void main() {" +
2014-06-20 01:54:14 +02:00
"gl_FragColor.a = smoothNoise(qt_TexCoord0 * virtual_resolution);" +
2014-06-11 00:54:47 +02:00
"}"
}
ShaderEffectSource{
id: staticNoiseSource
sourceItem: staticNoiseEffect
textureSize: Qt.size(parent.width, parent.height)
wrapMode: ShaderEffectSource.Repeat
smooth: true
hideSource: true
format: ShaderEffectSource.Alpha
}
//Rasterization mask
ShaderEffect {
id: rasterizationEffect
anchors.fill: parent
property size virtual_resolution: terminalContainer.virtual_resolution
2014-06-11 00:54:47 +02:00
blending: false
fragmentShader:
"uniform lowp float qt_Opacity;" +
"varying highp vec2 qt_TexCoord0;
uniform highp vec2 virtual_resolution;
float getScanlineIntensity(vec2 coords) {
float result = 1.0;" +
(mScanlines != shadersettings.no_rasterization ?
"result *= abs(sin(coords.y * virtual_resolution.y * "+Math.PI+"));" : "") +
(mScanlines == shadersettings.pixel_rasterization ?
"result *= abs(sin(coords.x * virtual_resolution.x * "+Math.PI+"));" : "") + "
return result;
}" +
"void main() {" +
"gl_FragColor.a = getScanlineIntensity(qt_TexCoord0);" +
"}"
}
ShaderEffectSource{
id: rasterizationEffectSource
sourceItem: rasterizationEffect
sourceRect: frame.sourceRect
hideSource: true
smooth: true
format: ShaderEffectSource.Alpha
}
}