Why does COSMOS crash after trying to draw a pixel? - c#

I'm am new to making Operating Systems with COSMOS. I am trying to add a pixel on the screen, but every time I open the GUI (done by typing "cos") it crashes. I have no idea why, but that's what happens.
Here is my code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Cosmos.System.Graphics;
using Sys = Cosmos.System;
namespace COS_v0._01
{
public class Kernel : Sys.Kernel
{
VGAScreen screen;
private static Canvas canvas;
public static void init()
{
canvas = FullScreenCanvas.GetFullScreenCanvas();
canvas.Mode = new Mode(1200, 700, ColorDepth.ColorDepth32);
}
protected override void BeforeRun()
{
Console.WriteLine("Welcome!");
init();
}
protected override void Run()
{
Pen pen = new Pen(Color.White);
canvas.DrawPoint(pen, 10, 10);
}
}
}

Related

Text and PlaceHolder stop working? is this intended on Custom Control based handlers?

So initially while I was creating my Custom Control I was facing an issue where the handlers were not calling the CreatePlatform method, I was able to solve that with some help from you guys more information: MAUI CreatePlatformView is never called?
But ever since I have done that it seems that a lot of properties of my Editor have stopped working which is really weird since they should by default get wired from the existing MAUI control.
Here is the relevant code:
Shared code handler:
using System;
using Microsoft.Maui.Handlers;
#if ANDROID
using NativeView = AndroidX.AppCompat.Widget.AppCompatEditText;
#endif
#if IOS
using NativeView = UIKit.UITextView;
#endif
public partial class FreakyEditorHandler : ViewHandler<IFreakyEditor, NativeView>
{
#region ctor
public static CommandMapper<IFreakyEditor, FreakyEditorHandler> CommandMapper = new(ViewCommandMapper);
public FreakyEditorHandler() : base(FreakyEditorMapper)
{
}
public FreakyEditorHandler(IPropertyMapper pmapper, CommandMapper cmapper = null)
: base(pmapper ?? FreakyEditorMapper, cmapper ?? CommandMapper)
{
}
#endregion
#region Mappers
public static IPropertyMapper<IFreakyEditor, FreakyEditorHandler> FreakyEditorMapper = new PropertyMapper<IFreakyEditor, FreakyEditorHandler>(ViewMapper)
{
[nameof(IFreakyEditor.HasUnderline)] = MapHasUnderlineWithColor,
[nameof(IFreakyEditor.UnderlineColor)] = MapHasUnderlineWithColor
};
public static void MapHasUnderlineWithColor(FreakyEditorHandler handler, IFreakyEditor entry)
{
handler.HandleNativeHasUnderline(entry.HasUnderline, entry.UnderlineColor);
}
#endregion
protected override void ConnectHandler(NativeView platformView)
{
base.ConnectHandler(platformView);
}
protected override void DisconnectHandler(NativeView platformView)
{
base.DisconnectHandler(platformView);
}
}
My Native Handlers are as follows:
Android:
using System;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.Content;
using AndroidX.Core.Content.Resources;
using AndroidX.Core.Graphics;
using AndroidX.Core.View;
using Microsoft.Maui.Handlers;
using Color = Microsoft.Maui.Graphics.Color;
namespace MAUI.FreakyControls
{
public partial class FreakyEditorHandler
{
protected override AppCompatEditText CreatePlatformView()
{
var _nativeView = new AppCompatEditText(Context)
{
ImeOptions = ImeAction.Done,
Gravity = GravityFlags.Top,
TextAlignment = Android.Views.TextAlignment.ViewStart,
};
_nativeView.SetSingleLine(false);
_nativeView.SetHorizontallyScrolling(false);
return _nativeView;
}
private void HandleNativeHasUnderline(bool hasUnderline, Color underlineColor)
{
ColorStateList colorStateList;
var AndroidColor = underlineColor.ToNativeColor();
colorStateList = ColorStateList.ValueOf(hasUnderline ? AndroidColor : Android.Graphics.Color.Transparent);
ViewCompat.SetBackgroundTintList(PlatformView, colorStateList);
//colorFilter = hasUnderline ? ContextCompat.GetColor(this, AndroidColor, PorterDuff.Mode.SrcAtop);
//BlendModeColorFilterCompat.CreateBlendModeColorFilterCompat(
// AndroidColor, BlendModeCompat.SrcAtop) :
//BlendModeColorFilterCompat.CreateBlendModeColorFilterCompat(
// Android.Graphics.Color.Transparent, BlendModeCompat.SrcAtop);
//PlatformView.Background?.SetColorFilter(colorFilter);
}
}
}
iOS :
using System;
using CoreGraphics;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using UIKit;
using CoreAnimation;
using static MAUI.FreakyControls.Platforms.iOS.NativeExtensions;
namespace MAUI.FreakyControls
{
public partial class FreakyEditorHandler
{
CALayer bottomline;
protected override UITextView CreatePlatformView()
{
var _nativeView = new UITextView();
return _nativeView;
}
private void HandleNativeHasUnderline(bool hasUnderline, Color color)
{
if (hasUnderline)
{
var uiColor = color.ToNativeColor();
bottomline = BottomLineDrawer(uiColor);
bottomline.Frame = new CGRect(x: 0, y: PlatformView.Frame.Size.Height - 5,
width: PlatformView.Frame.Size.Width, height: 1);
PlatformView.Layer.AddSublayer(bottomline);
PlatformView.Layer.MasksToBounds = true;
}
else
{
bottomline?.RemoveFromSuperLayer();
}
}
}
}
Handler registration :
handlers.AddHandler(typeof(FreakyEditor), typeof(FreakyEditorHandler));
You can access my code base here: https://github.com/FreakyAli/MAUI.FreakyControls/tree/r1-gh/feat/freakyeditor

Button classes that I've written with C# won't show up in Windows Form when running

Just started learning Visual Studio and I'm trying to make a soundboard in Windows Forms.
ButtonMaker(); is the function I use to make a button for each soundfile in my directory so I don't have to make 70 different buttons for every sound, but when I run the program nothing shows up in the forms window. Anybody know why? I've tried calling the function in Main() and in the initial Form1 class but nothing happens in either. Forms class file here:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MySoundBoard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
///Tried running it here
}
private void Form1_Load(object sender, EventArgs e)
{
///Tried running it here
}
private void ButtonMaker()
{
string[] files = Soundfiles.GetFile();
foreach (var item in files)
{
string btnName = item.ToUpper();
Button btNname = new Button();
btNname.Text = item;
int x = 40;
int y = 40;
btNname.Location = new Point(x, y);
x = x + 50;
if (x>900)
{
x = 40;
y = y + 30;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Here is the SoundFiles class:
using System.IO;
using System;
using System.Text;
using System.Diagnostics;
using WMPLib;
namespace MySoundBoard
{
class Soundfiles {
WMPLib.WindowsMediaPlayer Player;
static public string[] GetFile() {
string txtPath = #"C:\Documents\path\to\sound effects";
string[] files =
Directory.GetFiles(txtPath, "*ProfileHandler.cs", SearchOption.TopDirectoryOnly);
return files;
}
public void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.URL = url;
Player.controls.play();
}
}
}
And the main project file(not that worked with yet):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MySoundBoard
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
///Tried running it here
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
As said, I'm very new to this language and any help would be appriciated!
You create the buttons but never add them to the form. Just add
this.Controls.Add(btNname);
The next thing is, that your buttons will not do something. You'll need to add an event handler as well.
btNname.Click += ...;
In order to know which button plays which sound, you need to find a way to have that association. A hacky approach is
btNname.Tag = item;
and then evaluate Tag later

How to get finger positions in Leap Motion using C#

I want to get finger positions in Leap motion using c#.I'm new to leap motion controller. So i Couldn't find any example code for detecting finger positions, but i tried to come up with some code. I think it has lot of errors.
So, how to get finger positions in Leap Motion using C#?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Leap;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form, ILeapEventDelegate
{
private Controller controller;
private LeapEventListener listener;
public Form1()
{
InitializeComponent();
this.controller = new Controller();
this.listener = new LeapEventListener(this);
controller.AddListener(listener);
}
delegate void LeapEventDelegate(string EventName);
public void LeapEventNotification(string EventName)
{
if (!this.InvokeRequired)
{
switch (EventName)
{
case "onInit":
MessageBox.Show("onInit");
break;
case "onConnect":
MessageBox.Show("onConnect");
break;
case "onFrame":
MessageBox.Show("onFrame");
break;
}
}
else
{
BeginInvoke(new LeapEventDelegate(LeapEventNotification), new object[] {
EventName });
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public interface ILeapEventDelegate
{
void LeapEventNotification(string EventName);
}
public class LeapEventListener : Listener
{
ILeapEventDelegate eventDelegate;
public LeapEventListener(ILeapEventDelegate delegateObject)
{
this.eventDelegate = delegateObject;
}
public override void OnInit(Controller controller)
{
this.eventDelegate.LeapEventNotification("onInit");
}
public override void OnConnect(Controller controller)
{
this.eventDelegate.LeapEventNotification("onConnect");
}
public override void OnFrame(Controller controller)
{
this.eventDelegate.LeapEventNotification("onFrame");
}
public override void OnExit(Controller controller)
{
this.eventDelegate.LeapEventNotification("onExit");
}
public override void OnDisconnect(Controller controller)
{
this.eventDelegate.LeapEventNotification("onDisconnect");
}
}
}
Your code looks like it is derived from the documentation example. However, you have left out the actual event handlers so nothing will ever happen.
You will see in the linked example that there is a newFrameHandler function. You can change that to access the finger positions in the frame object, which is a snapshot of the tracked hands at a moment in time.
void newFrameHandler(Frame frame)
{
foreach(Finger in frame.Fingers){
Vector fingerPosition = finger.TipPosition;
//Do something with this information...
}
}
You can similarly get the hands and then access each hand's fingers, which often is the more natural approach:
foreach(Hand hand in frame.Hands){
foreach(Finger in hand.Fingers){
Vector fingerPosition = finger.TipPosition;
//Do something with this information...
}
}

How to create a Sleep method for my application

I want to create a method which makes my application wait X number of seconds, then continues on down a line of scripts. For example, this is the code that I have so far, after reading many similar help topics:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
methods.WriteTextToScreen(label1, "Hello!");
methods.sleepFor(1);
methods.WriteTextToScreen(label1, "Welcome!");
methods.sleepFor(1);
methods.WriteTextToScreen(label1, "Allo!");
}
public class methods
{
public static int timeSlept;
public static void WriteTextToScreen(Label LabelName, string text)
{
LabelName.Text = text;
}
public static void sleepFor(int seconds)
{
timeSlept = 0;
System.Timers.Timer newTimer = new System.Timers.Timer();
newTimer.Interval = 1000;
newTimer.AutoReset = true;
newTimer.Elapsed += new System.Timers.ElapsedEventHandler(newTimer_Elapsed);
newTimer.Start();
while (timeSlept < seconds)
{
Application.DoEvents();
}
Application.DoEvents();
}
public static void newTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timeSlept = IncreaseTimerValues(ref timeSlept);
Application.DoEvents();
}
public static int IncreaseTimerValues(ref int x)
{
int returnThis = x + 1;
return returnThis;
}
}
}
}
What I want to do is have my program do the methods.WriteTextToScreen(label1, "Hello!")
then wait for 1 second, then continue on in the same fashion. The problem is that the Form I'm displaying the text on doesn't show up at all until it has written "Allo!" onto the screen, so the first time it appears it already says that. Am I doing something wrong, or is there just no way to do this?
The form doesn't show until it has been constructed i.e. all the code in Form1 is run. See here for info on form constructors: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.form.aspx
To fix your problem you could move the writeTextToScreen and sleep code into the forms on load method. See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onload.aspx

Call a function from another class in C#

I am dealing with a new project. I have a class to draw Rectangles to windows form. I want to embed this class to another class. Code's below;
Main Code will call the shape code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using sekilciz_uygulama;
namespace xml_test_v1
{
class Program
{
static void Main(string[] args)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("c:\\sw_xml_test_4.xml");
int rad=0;
string giris_text = Console.ReadLine().ToString();
Console.WriteLine(giris_text);
foreach(XmlNode node in xDoc.SelectNodes("network/switch"))
{
string ip_adress = node.SelectSingleNode("ip_adress").InnerText.ToString();
Console.WriteLine(ip_adress);
if (ip_adress.Contains(giris_text))
{
// call for shape code!!!
}
}}}}
Code for Creating Shapes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace sekilciz_uygulama
{
public class Sekilciz
{
public Rectangle[] skare;
private SolidBrush firca;
private int x,y, genislik, yukseklik;
public Sekilciz()
{
skare = new Rectangle[5];
firca = new SolidBrush(Color.Blue);
x = 500;
y = 200;
genislik= 100;
yukseklik =100;
for(int i=0; i< skare.Length;i++)
{
skare[i] = new Rectangle(x,y,genislik,yukseklik);
x-=150;
}
}
public void kareciz(Graphics duzlem)
{
foreach(Rectangle rec in skare)
{
duzlem.FillRectangle(firca,rec);
}
}
}
}
var sekilciz = new Sekilciz();
sekilciz.kareciz(null);
You need to pass a parameter to your method kareciz
// "this" is your windows form, or control like button
var myGraphic = this.CreateGraphics();
var sekilciz = new Sekilciz(myGraphic);
sekilciz.kareciz();
But you have too many processing going on in your constructor. It is better to move that code in some other method in the same class.
Look here to complete sample on drawing on windows form:
The Basics of Drawing Graphics onto Windows Forms

Categories