Making a crowdsourced project on Canvas - page 23

 
Реter Konow:

))) So why did I start this topic? ) I'm going to study OOP now.


That's the advantage of your way - from practice to theory. I've done that way too. My gut used to resist the OOP paradigm for a long time, because I started programming when it didn't exist in the wild. But now I understand that this is the most valuable invention in programming. And sometimes it seems to me that if AOP hadn't been invented so far, I would have invented it myself :))
You, Peter, will like it and you will be delighted when you realize the awesomeness of OOP and what opportunities it opens. I assure you :)). And there is nothing complicated and tangled in OOP, especially for you with your programming experience. And it seems to me, in your case, the documentation of Metacvots may even be enough to get to the bottom of it. There is a lot of literature out there.
Tag Konow:

But Nikolay, I still do not understand what I was asking - is there a possibility to set a specific colour for gradient lines of the frame in CCcanvas class? Looking at your example, one could think that there is... If so, can you draw something similar to my example?

OK, OK. Although it's not a topic of this thread, but I'll try to clarify things that seem obvious to knowledgeable programmers, but not obvious to programmers encountering Canvas for the first time.
So, what is Canvas. Translated from English it's a canvas. It is simply any area of the screen within a window to which it is then attached. Its main characteristics are its size (Width and Height). And in fact, it is a one-dimensional array of points of type uint (4 bytes) with size Width*Height. In CCanvas class implementation this array is called m_pixels, and by default it's privat (which means accessible only inside class), but I personally always make it public (accessible not only inside class, but in user applications as well), because I want to operate with data array directly. Each point on this canvas can take any value of color type (also 4 bytes like uint, one byte reserved), which means a point of any RGB colour. Each colour (Red, Green or Blue) corresponds to one byte (256 values), so there are 256*256*256=16,777,216 RGB colours in total.

To put a red dot in say X,Y coordinates, where X can take values from 0 to (Width-1) and Y from 0 to (Height-1) just assign a red value to a cell of array m_pixels numbered (Y*Width+X) :

m_pixels[Y*Width+X]=clrRed;

or what is the same:

m_pixels[Y*Width+X] = 0x0000FF; // 0x00FF00 зеленый, - 0xFF0000 - синий, 0x00FFFF - желтый и т.д.

That's ALL!!!
You can now not even use built-in functions of CCanvas class (which is of course silly, because there is a lot of useful stuff there) . If you can put a dot on the screen anywhere and in any colour - you God what you want and draw it: buttons, graphics, create your own GDI, etc. etc.

And if anyone thinks that kanvas is slow, they're wrong. Kanvas is incredibly fast and it's a credit to the development team. I've tested kanvas drawing speed many times using Dll libraries written in C++ as well. Now after the updates of the last year the canvas on MT5 is almost as fast as if you have written it in Visual Studio in C++. It is only 10-15% slower. And don't forget - Windows is written in C++ and it's all Canvas. This is the beauty and promise of MT5 with its MQL5!!! Neither Java nor C# will be able to boast of such speed, as some have written in this thread.

Not for nothing developers have recently introduced a new property CHART_SHOW to disable rendering graph.
I'll demonstrate this with an example of a script:

//+------------------------------------------------------------------+
//|                                                  CleanScreen.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

// Внимание !!! В классе CCanvas массив m_pixels должен быть определен как public
#include <Canvas\Canvas.mqh>

void OnStart()
  {
   ChartSetInteger(0,CHART_SHOW,false); // очищаем экран от всего
   int Width =(int)ChartGetInteger(0,CHART_WIDTH_IN_PIXELS);  // получаем Ширину экрана
   int Height=(int)ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS); // получаем Высоту экрана

   CCanvas C;   // создаем объект Canvas
   if(!C.CreateBitmapLabel(0,0,"CleanScreen",0,0,Width,Height,COLOR_FORMAT_XRGB_NOALPHA)) // Создаем холст на весь экран
      Print("Error creating canvas: ",GetLastError());

// Теперь что-нибудь нарисуем на этом чистом экране
// например красную точку в центре экрана
   int X=Width/2;
   int Y=Height/2;
   C.m_pixels[Y*Width+X]=0xFF0000; // красный
                                   // или нарисуем окружность в центре с радиусом 100 по точкам фиолетового цвета
   int r=100;
   int q=(int)ceil(r*M_SQRT1_2);
   int r2=r*r;
   for(int x=0; x<=q; x++)
     {
      int y=(int)round(sqrt(r2-x*x));
      C.m_pixels[(Y+y)*Width+X+x]=0xFF00FF;
      C.m_pixels[(Y+y)*Width+X-x]=0xFF00FF;
      C.m_pixels[(Y-y)*Width+X+x]=0xFF00FF;
      C.m_pixels[(Y-y)*Width+X-x]=0xFF00FF;
      C.m_pixels[(Y+x)*Width+X+y]=0xFF00FF;
      C.m_pixels[(Y+x)*Width+X-y]=0xFF00FF;
      C.m_pixels[(Y-x)*Width+X+y]=0xFF00FF;
      C.m_pixels[(Y-x)*Width+X-y]=0xFF00FF;
     }
// конечно же можно окружность построить проще :) - через встроенную функцию класса CCanvas:
   C.Circle(X,Y,r+20,0xFFFF00); // желтого цвета
   C.Update(); // Теперь выводим это на экран
  }

Operation of this script:


The screen is cleared, but access to the quotes remains and we can use this blank canvas to make our own chart interface, such as this one with a gradient:


Regex Konow:

But Nikolai, I still haven't understood what I was asking about - is there an option in the CCcanvas class to set a specific colour to the gradient lines of the frame? Looking at your example, one would think that there is... If so, can you draw something similar to my example?

The colour is defined inside the GButton class, and there are even two functions for mixing the two colours and creating an array of colours to flow from one colour to another, i.e., just for the gradient. It's just a matter of defining the concept of a gradient. In my understanding, a gradient (or gradient fill) is a smooth flow of one colour to another. In your example there is no gradient, only 4 colours. In my example, inside the GButton class, 4 additional colours are formed with different brightness of the colour and an array of colours is formed for the gradient fill. This is done precisely to make life easier for the end user of the class. Why should he bother with the formation of these colours?
Draw something similar to your example - it's your homework, Peter :)) Come on, start to understand classes, hence the OOP.
Good luck!
 
Nikolai Semko:

Good luck!

OK, Nikolai. Thanks and good luck to you too!
 
Nikolai Semko:

Nikolai, I wanted to reply to your informative post yesterday, but I could not collect my thoughts. Now I have gathered my thoughts and realized that I need to write from my heart, not from my mind, otherwise I won't explain anything. Of course, it would be off-topic and the moderators would probably delete it, but still...

What you write about OOP is inspiring. I've already taken up studying it with the serious intention of mastering it. However, in the process of learning it, I was constantly bombarded with questions of the category "why take the long way when there's a shortcut?" I constantly saw easy, concise and clear ways of solving problems, and it was as if I was being told: "No! In OOP this is not a solution. You have to do it in a formal, formal and noble way." In response I asked myself, "But how can it be? I don't need those entities. Why should I add them to the code? They're superfluous. I want to compress the code." And I said, "No, it's not professional. That's bad code."

I was arguing much on the subject of OOP trying to figure out what its advantages were for me. I thought: "If I solve tasks perfectly well and easily without OOP, why should I use it? Why should I complicate additionally simple and clear to me things? How, where and why should I put something into my code that I don't need?". Then I decided that I don't need OOP because I do everything myself and I'm very good at it. When you do everything yourself and don't expect to plug anything and moreover you search for and find most effective solutions, then a natural question arises: "Why else? It would seem that OOP gives you comfort and order to a program. Yes, it does. But I have my own order. My own structure. My own entities. My own rules. So why should I reject it?

Just as our bodies reject alien tissue, so I could not "fuse" with the product of alien thought. Everywhere and in everything I felt rejected. For me, PLO was an order created by someone else's thought and it did not pass my efficacy test. It was this test that was the decisive factor for me. I realized that if I use OOP I will be a weaker programmer than if I go my own way and solve all the problems myself. My "programming strength" is that I write in a language I understand in Russian, use a shared global memory for graphical objects, which I call "kernel" and create large and complex mechanisms that work with this kernel. This is my approach.

I won't program OOP until someone defeats me with this approach. Until it does what I do, but even more effectively.

I asked about kanvas because I wanted to understand why my mechanism turns out to be more effective in fact. Not in theories, not in code beauty, but in practice.

Anyway, thank you Nikolay for inspiring and guiding me. ))

 
Реter Konow:

...

I won't program OOP until someone beats me with this approach.

...


That someone is probably one of those windmills you fight so frantically in your imagination. )
 
Anatoli Kazharski:

This someone must be one of those windmills you fight so frantically in your imagination. )
There you go! Trolls are coming...))
 
Реter Konow:
There you go! Trolls are coming...))
Calm down Peter. They're just mills. They just stand there, perform a certain function and are not trying to beat you. Though who knows how it's visualized in your fertile imagination. From all appearances, it looks very spectacular. )
 
Реter Konow:

I am absolutely not a fan of OOP, but you must know and be able to use it clearly if you consider yourself a programmer, at least for the sake of self-education.

But to use it or not to solve your application tasks is up to you.

 
Anatoli Kazharski:
Calm down Peter. They're just mills. They're just standing around, performing a certain function and not trying to beat you. Although who knows how it's being visualized in your fertile imagination. From all appearances, it looks very spectacular. )

Anatoly, mills or not, I like to fight and win. Especially at what I'm good at. I like live competition, duels and stuff... It's all part of evolution and natural selection. So it's okay... As long as the rules are fair.

Collaborating with like-minded people, participating in common projects, working as part of a team is also cool. And imagination, of course, must not replace reality. If that's what you mean, I agree.

 
Комбинатор:

... But you must know and be able to use it clearly if you consider yourself a programmer, at least for the sake of self-education.

But it is up to you to use it or not for your own applications.

One cannot disagree with this.
 
Реter Konow:

I won't program OOP until someone defeats me with this approach. Until they do with it what I do, but even more efficiently.

I asked about kanvas because I wanted to understand why my mechanism turns out to be more effective in fact. Not in theories or code beauty but in practice.

In any case, thank you Nikolay for inspiring and guiding me. ))

If two programmers with equal experience and intelligence are competing while creating similarlarge projects. But the first one uses only functions, while the second uses functions and classes. The second one will definitely win. But, I repeat, if it is a voluminous project, the latter will make it faster because there will be fewer bugs and more order. And the product of the second will be more readable, maintainable and more easily updated and supplemented. This isn't even my imho, it's just a statement of fact. It's easier to dig a hole with a shovel than with a trowel. If you implemented your mechanism not only on functions, but also on classes, it would become more efficient. That's my imho.

And, Peter, I took a quick look at your code and realized that you are using canvas to the fullest extent (though not CCanvas class, but who cares). Then why all these questions about canvas and why I'm trying to explain all these things here? :)).

Reason: